我想将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,但仍然无法正常工作...请提前帮助和感谢。
答案 0 :(得分:1)
Aspose Cells for Java 7.0已经过时了。当前有版本18.7。可能是7.0版与您的JDK版本不兼容。请尝试使用最新版本的Aspose Cells,看看问题是否解决。