将 MS Excel 工作表转换为图像

convert MS Excel Worksheet to image

小问题

我正在搜索可以将工作表转换为图像的 Java 库。

我的用例

我有一个 pptx 文件。该文件在一张幻灯片上嵌入了 excel 文件。在 "presentation modus" 中(我的意思是当您不编辑 excel 时),显示图像表示。该图像由 PowerPoint 生成并存储在 /ppt/images/*.emf 中的 pptx 文件中。如果 excel 文件的数据发生变化(这就是我以编程方式所做的),图像就不再是最新的了。因此,您必须生成工作表的新图像并将旧图像替换为新图像。

已找到选项

我想知道是否有其他选择。非常感谢任何提示。

您可以将 excel 文件转换为 HTML(例如 like this) and then convert it to an image (e.g with this library)。

另一种选择是 SmartXLS,但它在 java.awt.headless=true 模式下不起作用,这对我们来说是行不通的。

最后还是用aspose来做吧。
@spilymp 感谢支持

这里是片段,以防 aspose link 不再起作用:

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