导出工作表中的单元格范围为图像

Contents
[ ]

要获取范围的图像,请将打印区域设置为所需的范围,然后将所有页面边距设置为0。还应设置ImageOrPrintOptions.setOnePagePerSheet()true

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

输入文件

todo:image_alt_text

执行此代码会创建只包含范围E8:H10的图像。

输出图像

todo:image_alt_text

// 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");

您可能还会发现文章将工作表转换为不同的图像格式有用。