Short question
I'm searching a Java library which can convert a worksheet into an image.
My use case
I have a pptx file. That file has on one slide an excel file embedded. In "presentation modus" (I mean when you don't edit the excel), an image representation is shown. That image is generated by PowerPoint and stored in the pptx file in /ppt/images/*.emf. If the data of the excel file gets changed (that's what I do programmatically), the image isn't up to date anymore. So you have to generate a new image of the worksheet and replace the old image by the new one.
Already found Options
I wonder if there are alternatives. Any hint is really appreciated.
Another option is SmartXLS, but it doesen't work in java.awt.headless=true
modus and that's a no go for us.
Finally we will do it with aspose.
@spilymp Thanks for the support.
Here is the snipped that will do it just in case that the aspose link won't work anymore:
public void generateImages(final String sourcePath) {
try {
Workbook workbook = new Workbook(sourcePath);
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) {
String imageFilePath = sourcePath + "_output_" + (noOfImages++) + ".jpeg";
SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions());
sr.toImage(0, imageFilePath);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Returns all worksheets present in given workbook.
*
* @param workbook
* @return all worksheets present in given workbook.
*/
private List<Worksheet> getAllWorksheets(final Workbook workbook) {
List<Worksheet> worksheets = new ArrayList<Worksheet>();
WorksheetCollection worksheetCollection = workbook.getWorksheets();
for (int i = 0; i < worksheetCollection.getCount(); i++) {
worksheets.add(worksheetCollection.get(i));
}
return worksheets;
}
/**
* Returns ImageOrPrintOptions for png images
*
* @return
*/
private ImageOrPrintOptions getImageOrPrintOptions() {
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
imgOptions.setImageFormat(ImageFormat.getJpeg());
imgOptions.setOnePagePerSheet(true);
return imgOptions;
}