Home » excel » java – Generate Image from Excel sheet containg more than one sheet and bulk amount of data?

java – Generate Image from Excel sheet containg more than one sheet and bulk amount of data?

Posted by: admin May 14, 2020 Leave a comment

Questions:

I am generating image of excel sheet using aspose.cells and it is working fine but when I try to generate excel file containing 4 sheets and bulk amount of data in them it produces an error

OutOfHeapMemory

And if I put condition than it reads that excel which has less data like this-

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);
                                        }
                                    }
                                }
                            }

I have used System.out.println before and after if condition to check the worksheets that are iterating in loop.

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  ]
How to&Answers:

Please notice the following sample code for your reference, it will generate series of images for different pages of the first worksheet:

Sample code:

// 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.”);

If you still find any issue or confusion, kindly do provide your template file and paste your sample code (runnable) to show the issue, we will check it soon.

Note: I am working as Support developer/ Evangelist at Aspose.