使用 ImageOrPrintOptions 将工作表和工作簿呈现为图像

概述

有时,您可能需要将您的工作表呈现为图形表示。您确实需要将工作表图像显示到您的应用程序或网页中。您可能需要将图像插入到 Word 文档、PDF 文件、PowerPoint 演示文稿中,或者在其他一些场景中使用它们。您只需要将工作表呈现为图像,以便您可以在其他地方使用它。 Aspose.Cells 支持将Excel文件中的工作表转换为图片。此外,Aspose.Cells 支持设置不同的选项,如图像格式、分辨率(垂直和水平)、图像质量以及其他图像和打印选项。

API 提供了几个有价值的类,例如,图纸渲染, 图像或打印选项, 工作簿渲染, ETC。

图纸渲染类处理为工作表渲染图像的任务,而工作簿渲染对工作簿做同样的事情。上述两个类都有几个重载版本印象可以将工作表或工作簿直接转换为使用所需属性或选项指定的图像文件的方法。您可以将图像文件保存到磁盘/流中。支持多种图像格式,例如 BMP、PNG、GIFF、JPEG、TIFF、EMF 等。

将工作表转换为图像

// 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(ConvertWorksheettoImage.class) + "TechnicalArticles/";
//Instantiate a new Workbook object
//Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
//Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
//Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
//Set Horizontal Resolution
options.setHorizontalResolution(300);
//Set Vertical Resolution
options.setVerticalResolution(300);
//Set TiffCompression
options.setTiffCompression(TiffCompression.COMPRESSION_LZW);
//Set Image Format
options.setImageType(ImageType.TIFF);
//Set printing page type
options.setPrintingPage(PrintingPageType.DEFAULT);
//Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
//Render/save the image for the sheet
sr.toImage(0, dataDir + "CWorksheettoImage_out.tiff");

转换选项

可以将特定页面保存到图像中。以下代码将工作簿中的第一个和第二个工作表转换为 JPG 图像。

// 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(ConversionOptions.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Get the first worksheet
Worksheet sheet = book.getWorksheets().get(0);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render the sheet with respect to specified image/print options
SheetRender sr = new SheetRender(sheet, options);
// Render/save the image for the sheet
sr.toImage(0, dataDir + "ConversionOptions_out.jpg");

或者您可以循环浏览工作簿并将其中的每个工作表呈现为单独的图像:

// 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(WorksheetToSeparateImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
// Open template
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Iterate over all worksheets in the workbook
for (int i = 0; i < book.getWorksheets().getCount(); i++) {
Worksheet sheet = book.getWorksheets().get(i);
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Horizontal Resolution
options.setHorizontalResolution(300);
// Set Vertical Resolution
options.setVerticalResolution(300);
// Set Image Format
options.setImageType(ImageType.JPEG);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
SheetRender sr = new SheetRender(sheet, options);
sr.toImage(0, dataDir + "WSheetToSImage_out-" + sheet.getName() + ".jpg");
}

将工作簿转换为图像:

为了将完整的工作簿呈现为图像格式,您可以使用上述方法或简单地使用工作簿渲染接受实例的类工作簿以及对象图像或打印选项.

// 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(ConvertWorkbooktoImage.class) + "TechnicalArticles/";
// Instantiate a new Workbook object
Workbook book = new Workbook(dataDir + "book1.xlsx");
// Apply different Image and Print options
ImageOrPrintOptions options = new ImageOrPrintOptions();
// Set Image Format
options.setImageType(ImageType.TIFF);
// If you want entire sheet as a single image
options.setOnePagePerSheet(true);
// Render to image
WorkbookRender render = new WorkbookRender(book, options);
render.toImage(dataDir + "CWorkbooktoImage_out.tiff");

相关文章