首页 > 解决方案 > 如何在 netBeans 的现有项目中使用 Aspose.Cells?

问题描述

我想将 excel 工作表数据转换为图像,目前我正在使用 awt 的 2D 图形,但它没有给我想要的结果。我已经读过 aspose.cells 可用于转换为图像,但是当我尝试实现时,它给了我这样的错误-

Exception in thread "main" java.lang.NoSuchMethodError: sun.font.FontManager.getFontPath(Z)Ljava/lang/String;
    at com.aspose.cells.a.c.cf.p(Unknown Source)
    at com.aspose.cells.a.c.cf.<init>(Unknown Source)
    at com.aspose.cells.eY.<init>(Unknown Source)
    at com.aspose.cells.vm.a(Unknown Source)
    at com.aspose.cells.SheetRender.<init>(Unknown Source)
    at com.boolment.dataentry.ExcelTesting.generateImages(ExcelTesting.java:28)
    at com.boolment.dataentry.ExcelTesting.main(ExcelTesting.java:16)

我为转换文件编写的代码是-

package com.boolment.dataentry;

import com.aspose.cells.ImageFormat;
import com.aspose.cells.ImageOrPrintOptions;
import com.aspose.cells.SheetRender;
import com.aspose.cells.Workbook;
import com.aspose.cells.Worksheet;
import com.aspose.cells.WorksheetCollection;
import java.util.ArrayList;
import java.util.List;

public class ExcelTesting {
    public static void main(String s[]) throws Exception{
        ExcelTesting et = new ExcelTesting();       
        String sourcePath = "C:\\Users\\TANISHA AGARWAL\\Downloads\\EXCEL FORMAT.xls";
        et.generateImages(sourcePath);
    }

    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();  
     }  
 }  
    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;  
 }  
    private ImageOrPrintOptions getImageOrPrintOptions() {  
     ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();  
     imgOptions.setImageFormat(ImageFormat.getJpeg());  
     imgOptions.setOnePagePerSheet(true);  
     return imgOptions;  
 }  
}

我已经包含了 aspose.cells 7.0 jar,但它仍然无法正常工作...请帮助并提前致谢。

标签: javaexcelimage-conversionaspose-cellsnetbeans-8.2

解决方案


Aspose Cells for Java 7.0 已经过时了。当前版本为 18.7。可能是版本 7.0 与您的 JDK 版本不兼容。请尝试使用最新版本的 Aspose Cells,看看问题是否解决。


推荐阅读