将工作表转换为图像以及按页将工作表转换为图像

使用 Aspose.Cells 将工作表转换为图像文件

本文介绍如何使用 Aspose.Cells for Java API 将工作表转换为图像。API 提供了一些有价值的类,比如 SheetRenderImageOrPrintOptionsWorkbookRender 等。 SheetRender 类表示要为其渲染图像的工作表,并具有重载的 toImage 方法,可以直接将工作表转换为图像文件,而无需设置任何属性或选项。

// 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(ConvertWorksheettoImageFile.class) + "TechnicalArticles/";
// Create a new Workbook object
// Open a template excel file
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Define ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Specify the image format
imgOptions.setImageType(ImageType.JPEG);
// Render the sheet with respect to specified image/print options
SheetRender render = new SheetRender(sheet, imgOptions);
// Render the image for the sheet
render.toImage(0, dataDir + "CWToImageFile.jpg");

结果

执行上述代码后,名为Sheet1的工作表将被转换为图像文件SheetImage.jpg。

JPG输出

todo:image_alt_text

使用Aspose.Cells按页将工作表转换为图像文件

此示例演示如何使用Aspose.Cells将模板工作簿中具有多个页面的工作表转换为每页一个图像文件。

// 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(ConvertWorksheetToImageByPage.class) + "TechnicalArticles/";
// Create a new Workbook object
// Open a template excel file
Workbook book = new Workbook(dataDir + "ConvertWorksheetToImageByPage.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Define ImageOrPrintOptions
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Resolution
options.setHorizontalResolution(200);
options.setVerticalResolution(200);
options.setImageType(ImageType.TIFF);
// Sheet2Image by page conversion
SheetRender render = new SheetRender(sheet, options);
for (int j = 0; j < render.getPageCount(); j++) {
render.toImage(j, dataDir + sheet.getName() + " Page" + (j + 1) + ".tif");
}

结果

执行上述代码后,名为Sheet1的工作表将转换为两个图像文件(每页一个)Sheet 1 Page 1.Tiff和Sheet 1 Page 2.Tiff。

生成的图像文件(Sheet 1 Page 1.Tiff)

todo:image_alt_text

生成的图像文件(Sheet 1 Page 2.Tiff)

todo:image_alt_text

相关文章

  • 将工作表转换为不同的图像格式
  • 使用所需的宽度和高度将工作表或图表导出为图像