我正在使用aspose.cells生成Excel工作表的图像,并且工作正常,但是当我尝试生成包含4张工作表和大量数据的Excel文件时,会产生错误
OutOfHeapMemory
如果我把条件设置成比它读取的excel少,它的数据就少了,像这样-
Workbook workbook = new Workbook(sourceDir);
List<Worksheet> worksheets = getAllWorksheets(workbook);
if (worksheets != null) {
int noOfImages = 0;
for (Worksheet worksheet : worksheets) {
if (worksheet.getCells().getCount() > 0 || worksheet.getCharts().getCount() > 0 || worksheet.getPictures().getCount() > 0) {
System.out.println("Worksheet Before Condition" + worksheet);
if (worksheet.getCells().getCount() < 2000) {
System.out.println("Worksheet After Condition" + worksheet);
String imageFilePath = workingDir + File.separator + projectName + File.separator + fileName + "_" + (noOfImages++) + ".png";
SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions());
sr.toImage(0, imageFilePath);
}
}
}
}
在if条件之前和之后,我已经使用 System.out.println 来检查正在循环的工作表。
Worksheet Before ConditionAspose.Cells.Worksheet[ Details of Indian Companies Reg ]
Worksheet Before ConditionAspose.Cells.Worksheet[ LLP'S Incorporated in Jun ]
Worksheet Before ConditionAspose.Cells.Worksheet[ Foreign Companies Registered ]
Worksheet After ConditionAspose.Cells.Worksheet[ Foreign Companies Registered ]
答案 0 :(得分:2)
请注意以下示例代码供您参考,它将为第一张工作表的不同页面生成一系列图像:
示例代码:
// Instantiate a new workbook with path to an Excel file
Workbook book = new Workbook(dataDir + “MyTestBook1.xlsx”);
// Create an object for ImageOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageFormat(ImageFormat.getPng());
// Get the first worksheet.
Worksheet sheet = book.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int j = 0; j < sr.getPageCount(); j++) {
// Generate an image for the worksheet
sr.toImage(j, dataDir + “WToImage-out” + j + “.png”);
}
// Print message
System.out.println(“Images generated successfully.”);
如果您仍然发现任何问题或困惑,请提供您的模板文件并粘贴示例代码(可运行)以显示问题,我们将尽快对其进行检查。
注意:我是Aspose的支持开发人员/传播者。