1

我想将我的 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 大小或单张 1 取决于值

imgOptions.setOnePagePerSheet(true);

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

4

1 回答 1

0

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

于 2015-09-02T07:11:06.327 回答