Is it possible to convert an Excel workbook to PNG with visible row and column headers?
Yes, it is possible to convert an Excel workbook to PNG format while keeping the row and column headers visible. You can achieve this using the Aspose.Cells API. Below is a sample code snippet that demonstrates how to perform this conversion:
import com.aspose.cloud.cells.client.*;
import com.aspose.cloud.cells.model.*;
import com.aspose.cloud.cells.request.*;
import java.io.File;
import java.util.HashMap;
public class ConvertExcelToPng {
private CellsApi api;
public ConvertExcelToPng() {
try {
api = new CellsApi(CellsApiUtil.GetClientId(), CellsApiUtil.GetClientSecret(), CellsApiUtil.GetAPIVersion(), CellsApiUtil.GetBaseUrl());
} catch (ApiException e) {
e.printStackTrace();
}
}
public void convertWorkbookToPng(String localFilePath, String remoteFileName) throws Exception {
// Upload the Excel file to the server
CellsApiUtil.Upload(api, "TestData/In/" + remoteFileName, localFilePath, "");
// Create a request to convert the workbook
PutConvertWorkbookRequest request = new PutConvertWorkbookRequest();
request.setFormat("png"); // Set the format to PNG
// Prepare the file map for the request
HashMap<String, File> fileMap = new HashMap<>();
fileMap.put(remoteFileName, CellsApiUtil.GetFileHolder(localFilePath));
request.setFile(fileMap);
// Perform the conversion
File response = this.api.putConvertWorkbook(request);
System.out.println("Workbook converted to PNG successfully: " + response.getPath());
}
public static void main(String[] args) {
ConvertExcelToPng converter = new ConvertExcelToPng();
try {
converter.convertWorkbookToPng("path/to/your/excel/file.xlsx", "file.xlsx");
} catch (Exception e) {
e.printStackTrace();
}
}
}
In this code:
- You first upload the Excel file to the server.
- Then, you create a request to convert the workbook to PNG format.
- Finally, you execute the conversion and handle the response.
Make sure to replace "path/to/your/excel/file.xlsx"
with the actual path to your Excel file. This will ensure that the row and column headers are included in the PNG output.
For more details, you can refer to the Aspose.Cells Cloud API documentation for additional options and configurations.
Sources:
[1]: Aspose.Cells Cloud API Documentation
@Professionalize.Discourse thanks for the quick response. I’m using the same method in Python but that returns an image that does not have row and column headers (i.e., A, B, …, and 1, 2, …). I don’t see in your code anything setting header visibility (something like is_row_column_headers_visible).
Here’s my code
api = CellsApi(client_id, client_secret, "v3.0", base_url)
source_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), path, file_name)
request = PutConvertWorkbookRequest(file=source_file, format='png')
temp_file = api.put_convert_workbook(request)
What should I do to get an image with row and column headers?
Please refer to the following code:
cellsApi = CellsApi('YourClientId','YourClientSecret') convertOption = ConvertWorkbookOptions () convertOption.convert_format = 'png' convertOption.file_info = FileInfo() convertOption.file_info.filename = "Book1.xlsx" with open("TestData/Book1.xlsx", "rb") as excel_file: convertOption.file_info.file_content = base64.b64encode(excel_file.read()).decode("utf-8") convertOption.page_setup = PageSetup() convertOption.page_setup.print_headings = True convertWorkbookRequest = PostConvertWorkbookRequest(convertOption) fileInfo = cellsApi.post_convert_workbook(convertWorkbookRequest) file_bytes = base64.b64decode(fileInfo.file_content) with open(fileInfo.filename, "wb") as f: f.write(file_bytes)
@gertstahl
You also can update page setup setting about header and print gridline in MS Excel.
image.png (3.9 KB).
Then you can simply convert Excel to png with row/column headers without calling codes about PageSetup() to update them.