简短的问题
我正在搜索一个可以将工作表转换为图像的Java库。
我的用例
我有一个 pptx 文件。该文件在一张幻灯片上嵌入了一个 Excel 文件。在“演示模式”中(我的意思是当您不编辑 Excel 时),将显示图像表示。该图像由 PowerPoint 生成并存储在 /ppt/images/*.emf 中的 pptx 文件中。如果 Excel 文件的数据发生更改(这就是我以编程方式执行的操作),则图像将不再是最新的。因此,您必须生成工作表的新图像并用新图像替换旧图像。
已找到选项
我想知道是否还有其他选择。任何提示真的很感激。
另一个选项是 SmartXLS,但它在
java.awt.headless=true
模式下不起作用,这对我们来说是不行的。
最后我们将使用 aspose 来完成。
@spilymp 感谢您的支持。
这里是一个片段,可以在 aspose 链接不再工作的情况下执行此操作:
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;
}
Excel 工作表到图像
/** .xls 转 .png */
try {
// Specify the directory where you want to save the images
String imagepath = "/home/java/Downloads/";
// Load the Excel file (.xls)
com.aspose.cells.Workbook workbook = new com.aspose.cells.Workbook("/home/java/Downloads/test/84373.789909309331709719839016/QABxls.xls", new LoadOptions(com.aspose.cells.LoadFormat.EXCEL_97_TO_2003));
// Create an object of ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the output image type
imgOptions.setImageType(com.aspose.cells.ImageType.PNG);
// Get the number of worksheets in the workbook
int sheetCount = workbook.getWorksheets().getCount();
// Find the first visible worksheet
Worksheet visibleSheet = null;
for (int i = 0; i < sheetCount; i++) {
Worksheet sheet = workbook.getWorksheets().get(i);
if (sheet.isVisible()) {
visibleSheet = sheet;
break;
}
}
UUID randomId = UUID.randomUUID();
if (visibleSheet != null) {
// Create a SheetRender object for the first visible sheet
SheetRender sr = new SheetRender(visibleSheet, imgOptions);
// Generate images for all pages of the visible worksheet
for (int page = 0; page < 1; page++) {
// Generate an image for the worksheet
sr.toImage(page, imagepath + "visible_worksheet_page_" + randomId + ".png");
}
System.err.println("Images created for the first visible worksheet.");
} else {
System.err.println("No visible worksheet found in the workbook.");
}
} catch (Exception e) {
System.err.println(e);
}