将MS Excel工作表转换为图像 [英] convert MS Excel Worksheet to image

查看:42
本文介绍了将MS Excel工作表转换为图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短问题

我正在搜索一个Java库,该库可以将工作表转换为图像.

I'm searching a Java library which can convert a worksheet into an image.

我的用例

我有一个pptx文件.该文件在一张幻灯片上嵌入了一个excel文件.在表示方式"中(我的意思是当您不编辑Excel时),将显示图像表示.该图像由PowerPoint生成,并存储在/ppt/images/*.emf中的pptx文件中.如果excel文件的数据被更改(这是我通过编程方式执行的操作),则图像不再是最新的.因此,您必须生成工作表的新图像,并用新图像替换旧图像.

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.

已经找到的选项

  • 使用 iText 将excel转换为pdf,然后将pdf转换为图像
  • 摆放
  • 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.

推荐答案

另一个选项是 SmartXLS ,但在 java.awt.headless = true 方式下不起作用,对我们来说这是不行的.

Another option is SmartXLS, but it doesen't work in java.awt.headless=true modus and that's a no go for us.

最后,我们将使用aspose进行操作.
@spilymp感谢您的支持.

Finally we will do it with aspose.
@spilymp Thanks for the support.

如果aspose链接不再起作用,则将在此处执行以下操作:

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

这篇关于将MS Excel工作表转换为图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆