Dear Concern,
I am using aspose-cells-8.1.2.jar to convert the sheets of xlsx/xls file into jpeg image, but i am only able to convert the sheets whose name is not the default anme like “Sheet1” or “Sheet2” or so on, for the sheets having this name i am getting Blank jpg file of 0 KB.
If i rename the sheet name to “Sheet1” to “Testsheet” it works.
Please suggest…!!
PFA the xlsx i am testing.
Below is the code -
Workbook workbook = new Workbook(inputFileName);
WorksheetCollection worksheets = workbook.getWorksheets();
int sheets=worksheets.getCount();
for(int i = 0; i < sheets; i++)
{
ImageOrPrintOptions options = new ImageOrPrintOptions();
Worksheet sheet = worksheets.get(i);
options.setAllColumnsInOnePagePerSheet(true);
options.setImageFormat(ImageFormat.getJpeg());
SheetRender sr = new SheetRender(sheet, options);
System.out.println(“Name:”+sheet.getName());
try
{
sr.toImage(i, “test”+i+".jpg");
}
catch(Exception e)
{
e.printStackTrace();
}
Hi,
Thanks for the template file and sample code.
After an initial test, I observed the issue as you mentioned. I found Aspose.Cells
renders a blank image (0 bytes) for the Sheet e.g Sheet1 in the workbook
(Sheet to Image conversion). I used the template file provided by you with the following sample code:
e.g
Sample code:
Workbook workbook = new Workbook("DEPORTES+NATIONAL+Copy+Account+List+Report.xlsx");
WorksheetCollection worksheets = workbook.getWorksheets();
int sheets=worksheets.getCount();
for(int i = 0; i < sheets; i++)
{
ImageOrPrintOptions options = new ImageOrPrintOptions();
Worksheet sheet = worksheets.get(i);
options.setAllColumnsInOnePagePerSheet(true);
options.setImageFormat(ImageFormat.getJpeg());
SheetRender sr = new SheetRender(sheet, options);
System.out.println("Name:"+sheet.getName());
try
{
sr.toImage(i, "outtest1"+i+".jpg");
}
catch(Exception e)
{
e.printStackTrace();
}
}
I have logged a ticket with an id “CELLSJAVA-41219” for your issue. We will look into it soon.
Once we have any update on it, we will let you know here.
Thank you.
Hi Ishan,
Please replace the statement sr.toImage(i, “test”+i+".jpg"); with sr.toImage(0, “test”+i+".jpg"); and let us know of your results. Please note that the first parameter to SheetRender.toImage method is 0 index based page number. Your current code generates an empty Jpeg file for the second worksheet because during the second iteration, the value of i is 1 whereas with ImageOrPrintOptions.AllColumnsInOnePagePerSheet property set to true, the second worksheet renders on one page only.
Java
Workbook workbook = new Workbook(“DEPORTES+NATIONAL+Copy+Account+List+Report.xlsx”);
WorksheetCollection worksheets = workbook.getWorksheets();
int sheets=worksheets.getCount();
for(int i = 0; i < sheets; i++)
{
ImageOrPrintOptions options = new ImageOrPrintOptions();
Worksheet sheet = worksheets.get(i);
options.setAllColumnsInOnePagePerSheet(true);
options.setImageFormat(ImageFormat.getJpeg());
SheetRender sr = new SheetRender(sheet, options);
System.out.println(“Name:”+sheet.getName());
try
{
sr.toImage(0, “out”+i+".jpg");
}
catch(Exception e)
{
e.printStackTrace();
}
}
Hi,
Furthermore, since your first worksheet has long list of data which renders multiple pages for about 400 records(rows). We recommend you to just try to replace the following lines of code accordingly, so you could render all your images for the first worksheet as well:
e.g
Sample code:
…
try
{
sr.toImage(i, “test”+i+".jpg");
}
catch(Exception e)
{
e.printStackTrace();
}
with:
…
try
{
for(int j = 0; j < sr.getPageCount(); j++)
{
sr.toImage(j, “test”+ sheet.getName() + “_” + j + “.jpg”);
}
}
catch(Exception e)
{
e.printStackTrace();
}
it will work fine for your needs.
Let us know if you still have any confusion.
Thank you.