Excel图像转换

我想将我的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); 

任何人都可以告诉我如何自定义输出图像文件的大小?

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