Xlsx to PNG java.lang.OutOfMemoryError: Java heap space

While attempting to convert an xlsx workbook to a png format using the ‘SheetRender’ I am experiencing a java.lang.OutOfMemoryError: Java heap space, see below.


java.lang.OutOfMemoryError: Java heap space
at java.awt.image.DataBufferInt.(DataBufferInt.java:75)
at java.awt.image.Raster.createPackedRaster(Raster.java:467)
at java.awt.image.DirectColorModel.createCompatibleWritableRaster(DirectColorModel.java:1032)
at java.awt.image.BufferedImage.(BufferedImage.java:333)
at com.aspose.cells.b.a.b.za.(Unknown Source)
at com.aspose.cells.a.d.zcc.a(Unknown Source)
at com.aspose.cells.zbzd.a(Unknown Source)
at com.aspose.cells.zbzd.a(Unknown Source)
at com.aspose.cells.SheetRender.toImage(Unknown Source)

I am working with java version “1.8.0_111” and i have attached the method code.

The image resolution is set to 200 x 200 dpi.
All the columns must fit on one image but rows can overflow to other images.
The heap is currently at 2GB


Some additional information: Setting the heap to 4GB allows the process to finish but it takes 15 minuets to finish which is totally unacceptable.


Without having to sacrifice the image quality, increasing the heap size and having to wait long time to compleate, what options are available?

Hi,


Thanks for your posting and using Aspose.Cells.

If your worksheet has lots of pages probably more than 10 or 20 or 30, then you are going to get out of memory exception. Because lots of pages cannot be converted into a single page. In your case, lots of columns can also not be converted into a single page. So please share your excel file so that we could look into it further. We will investigate it and update you asap.

Please also download and try the latest version and see if it makes any difference in resolving your issue.



I want to generate image from excel having bulk amount of data for eg. 10000 data in one sheet so how can I generate it in different pages as image cannot load that much amount of data.

@tanishaa32,

You can convert a worksheet to image with SheetRender object. See the document with example code for your reference:

If you face any problem then let us know your feedback while sharing input and output files with us so that we can help you accordingly.

I have tried it but it only gives data upto 51 rows and after that it stops. I want that after that it generates in another page the next amount of data.

@tanishaa32,

Please notice the following sample code (especially the lines in bold) for your reference, it will generate series of images for different pages of the first worksheet:
e.g
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.

Thank you …it worked:grinning::+1: :grinning:

@tanishaa32,

Good to know that the suggested code figures out your issue now. Feel free to contact us any time if you need further help or have some other issue or queries, we will be happy to assist you soon.

I wan to decrypt an excel file in java…How can I do this?

@tanishaa32,

Please set the Password to an empty string, this should remove the encryption set on loading the spreadsheet. Please check the following piece of code for your reference.

LoadOptions options = new LoadOptions();
options.setPassword("1234");
Workbook book = new Workbook("book1.xlsx" , options);
book.getSettings().setPassword("");
book.save("output.xlsx");

I want to know that if I use this decryption method for decrypting excel file…why it is not working and same is working for doc file.

private String decrypt1(InputStream encrypteddata) throws IOException, IllegalBlockSizeException, BadPaddingException {
try {
byte k[] = “NiTh5252”.getBytes();
byte[] bytes = IOUtils.toByteArray(encrypteddata);
SecretKeySpec key = new SecretKeySpec(k, “DES”);
Cipher enc = Cipher.getInstance(“DES/ECB/NoPadding”);
enc.init(Cipher.DECRYPT_MODE, key);
byte[] encryptedText = new BASE64Decoder().decodeBuffer(Arrays.toString(bytes));
System.out.println(“com.boolment.dataentry.DataEntryArea1.decrypt1()” + encryptedText);
byte[] textDecrypted = enc.doFinal(bytes);
System.out.append(“Decrypted Text is :” + new String(textDecrypted));
return new String(textDecrypted);
} catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException e) {
e.printStackTrace();
}
return null;
}

@tanishaa32,

Please explain how you are Encrypting the Excel file that you are trying to Decrypt. Both Aspose.Words and Aspose.Cells APIs are diverse with different architectures so we cannot parse one’s object into other directly. So the code that is working for Aspose.Words API may not work with Excel document. Please provide your encrypted Excel file and mention how you have encrypted it. Also provide your decrypted file for investigations.

Encryption

public boolean encryptMethod(String projectName) throws FileNotFoundException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IOException {
File theDirpdf = new File(“Encrypt”.concat(projectName));
theDirpdf.mkdir();
File file1 = new File(workingDir + File.separator + projectName);
File[] fileslist = file1.listFiles();
for (File filename : fileslist) {
String str = filename.getName();
FileOutputStream fileoust;
try {
FileInputStream fiss = new FileInputStream(filename.getAbsolutePath());
fileoust = new FileOutputStream(new File(workingDir + File.separator + “Encrypt”.concat(projectName) + File.separator + str));
byte k[] = “NiTh5252”.getBytes();
SecretKeySpec key = new SecretKeySpec(k, “DES”);
Cipher enc = Cipher.getInstance(“DES”);
enc.init(Cipher.ENCRYPT_MODE, key);
CipherOutputStream cos = new CipherOutputStream(fileoust, enc);
byte[] buf = new byte[1024];
int read;
while ((read = fiss.read(buf)) != -1) {
cos.write(buf, 0, read);
}
fileoust.flush();
fileoust.close();
} catch (Exception e) {
e.printStackTrace();
}

    }
    return true;
}

@tanishaa32,

Your code does not appear to be working with Aspose.Cells API so this is certainly not an issue related with the APIs. You are encrypting Excel document not like password protecting it with Aspose.Cells API. So you may devise your own algorithm to encrypt or decrypt the file because Aspose.Cells implements its own algorithms to work with files.