将工作表中的 Cells 范围导出到图像

Contents
[ ]

要拍摄某个范围的图像,请将打印区域设置为所需范围,然后将所有边距设置为 0。同时设置ImageOrPrintOptions.setOnePagePerSheet() 方法真的.

以下代码获取范围 E8:H10 的图像。下面是代码中使用的源工作簿的屏幕截图。您可以使用任何工作簿试用代码。

输入文件

待办事项:图片_替代_文本

执行代码仅创建 E8:H10 范围的图像。

输出图像

待办事项:图片_替代_文本

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ExportRangeofCells.class) + "TechnicalArticles/";
// Create workbook from source file.
Workbook workbook = new Workbook(dataDir + "book1.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Set the print area with your desired range
worksheet.getPageSetup().setPrintArea("E8:H10");
// Set all margins as 0
worksheet.getPageSetup().setLeftMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
// Set OnePagePerSheet option as true
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setOnePagePerSheet(true);
options.setImageType(ImageType.JPEG);
// Take the image of your worksheet
SheetRender sr = new SheetRender(worksheet, options);
sr.toImage(0, dataDir + "ERangeofCells_out.jpg");

您还可以找到这篇文章将工作表转换为不同的图像格式有帮助。