convert MS Excel Worksheet to image

馋奶兔 提交于 2019-12-12 00:16:14

问题


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

  • convert the excel to pdf with iText and convert the pdf to an image
  • Aspose

I wonder if there are alternatives. Any hint is really appreciated.


回答1:


You can convert your excel file to HTML (e.g. like this) and then convert it to an image (e.g with this library).




回答2:


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


来源:https://stackoverflow.com/questions/31937106/convert-ms-excel-worksheet-to-image

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!