java - 如何在 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,但它仍然无法正常工作...请帮助并提前致谢。
解决方案
Aspose Cells for Java 7.0 已经过时了。当前版本为 18.7。可能是版本 7.0 与您的 JDK 版本不兼容。请尝试使用最新版本的 Aspose Cells,看看问题是否解决。
推荐阅读
- django - Django 正文编码与 slack-api 的秘密
- sql-workbench-j - sql workbench/j 不会在数据库资源管理器中显示任何对象
- swift - 在 Swift 中访问“级别”范围以获得更有意义的调试字符串
- c# - 在 xunit 中为不同的程序模式重新运行测试组
- html - 使用 + 选择器在另一个元素中以不同的方式设置相同的类
- reactjs - 在 reducer 中更新状态不会传播到组件
- c# - 如何从另一个表单中隐藏按钮
- sql-server - 使用左连接/外连接连接多个表
- ruby - 使用全局变量调用函数
- jedis - redis.clients.jedis.exceptions.JedisDataException:ERR 值不是整数或超出范围