java - Excel 到图像转换

标签 java excel aspose-cells

我想将我的 Excel 文件及其实体(图表、表格、图像)转换为 jpeg/png 图像。目前正在使用 aspose。这是我的代码

public static int excelToImages(final String sourceFilePath, final String outFilePrefix) throws Exception {
    int noOfImages = 0;

    Workbook workbook = getWorkbook(sourceFilePath);
    List<Worksheet> worksheets = getAllWorksheets(workbook);

    if (worksheets != null) {
        for (Worksheet worksheet : worksheets) {
            if (worksheet.getCells().getCount() > 0) {
                String outFilePath = FileUtils.getAbsoluteFilePath(outFilePrefix + (noOfImages++));

                SheetRender sr = new SheetRender(worksheet, getImageOrPrintOptions());
                sr.toImage(0, outFilePath);
            }
        }
    }
    return noOfImages;
}

private static ImageOrPrintOptions getImageOrPrintOptions() {
    ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
    imgOptions.setImageFormat(ImageFormat.getJpeg());
    imgOptions.setOnePagePerSheet(true);
    return imgOptions;
}

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

我的问题是输出图像的尺寸要么分割成多个 A4 尺寸,要么分割成一张,具体取决于

的值
imgOptions.setOnePagePerSheet(true);

谁能告诉我如何自定义输出图像文件的大小?

最佳答案

您可以使用imgOptions.setOnlyArea(true);来尝试。这会将图像的大小设置为将所有内容放入图像所需的最小值。但我不确定生成的图像是否被分割成 A4 部分。

关于java - Excel 到图像转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17545307/

相关文章:

java - 通过 Java 中的 selenium Web 驱动程序处理另存为对话框

c# - DAO 只负责 CRUD 操作?

excel - 更改列宽和行高

java - 是否可以解锁 protected 工作表中的特定单元格(使用 Aspose)

Excel 周列按升序排序

java - 获取 Excel 列的宽度(以像素为单位,而不是默认的测量单位)

java - 访问 WEB-INF 文件夹中子文件夹中的 JSP?

java - 在 Java 和 Android Studio 中检查当前时间与特定时间

excel - 将多个工作簿合并为一个工作簿,并将所有工作簿作为工作表

c# - Excel - 列宽与多个相加不匹配