将工作表转换为不同的图像格式
将工作表转换为图像
有时保存工作表图片是很有用的。图片可以在线共享,插入到其他文档(例如用Microsoft Word编写的报告或PowerPoint演示文稿)。
Aspose.Cells通过SheetRender类提供图像导出功能。该类表示将呈现为图像的工作表。SheetRender类提供了将工作表转换为图像文件的toImage()方法。支持BMP、PNG、JPEG、TIFF和EMF格式。
下面的代码显示了如何将Microsoft Excel文件中的工作表转换为PNG文件。
// 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(WorksheetToImage.class) + "LoadingSavingConvertingAndManaging/"; | |
// Instantiate a new workbook with path to an Excel file | |
Workbook book = new Workbook(dataDir + "MyTestBook1.xlsx"); | |
// Create an object for ImageOptions | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
// Set the image type | |
imgOptions.setImageType(ImageType.PNG); | |
// Get the first worksheet. | |
Worksheet sheet = book.getWorksheets().get(0); | |
// Create a SheetRender object for the target sheet | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
for (int j = 0; j < sr.getPageCount(); j++) { | |
// Generate an image for the worksheet | |
sr.toImage(j, dataDir + "WToImage-out" + j + ".png"); | |
} |
将工作表转换为SVG
SVG代表可伸缩矢量图形。SVG是基于XML标准的二维矢量图形规范。它是一个开放标准,自1999年由万维网联盟(W3C)开发。
自 v7.1.0 发布以来,Aspose.Cells for Java 可以将工作表转换为 SVG 图像。
要使用此功能,您需要在程序或项目中导入com.aspose.cells命名空间。它有一些有价值的类用于呈现和打印,例如SheetRender、ImageOrPrintOptions、WorkbookRender等。
com.aspose.cells.ImageOrPrintOptions类指定工作表将以SVG格式保存。
SheetRender类以ImageOrPrintOptions的对象作为参数,设置保存格式为SVG格式。
下面的代码片段显示了如何将Excel文件中的工作表转换为SVG图像文件。
// 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(ConvertingWorksheetToSVG.class) + "loading_saving/"; | |
String path = dataDir + "Book1.xlsx"; | |
// Create a workbook object from the template file | |
Workbook workbook = new Workbook(path); | |
// Convert each worksheet into svg format in a single page. | |
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions(); | |
imgOptions.setSaveFormat(SaveFormat.SVG); | |
imgOptions.setOnePagePerSheet(true); | |
// Convert each worksheet into svg format | |
int sheetCount = workbook.getWorksheets().getCount(); | |
for (int i = 0; i < sheetCount; i++) { | |
Worksheet sheet = workbook.getWorksheets().get(i); | |
SheetRender sr = new SheetRender(sheet, imgOptions); | |
for (int k = 0; k < sr.getPageCount(); k++) { | |
// Output the worksheet into Svg image format | |
sr.toImage(k, path + sheet.getName() + k + "_out.svg"); | |
} | |
} | |
// Print message | |
System.out.println("Excel to SVG conversion completed successfully."); |
渲染工作簿中的活动工作表
将工作簿中的活动工作表转换成SVG的简单方法是设置活动工作表索引,然后将工作簿保存为SVG。这将把活动工作表呈现为SVG。以下示例代码演示了这个特性:
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String outputDir = Utils.Get_OutputDirectory(); | |
// Instantiate a workbook | |
Workbook workbook = new Workbook(); | |
// Put sample text in the first cell of first worksheet in the newly created workbook | |
workbook.getWorksheets().get(0).getCells().get("A1").setValue("DEMO TEXT ON SHEET1"); | |
// Add second worksheet in the workbook | |
workbook.getWorksheets().add(SheetType.WORKSHEET); | |
// Set text in first cell of the second sheet | |
workbook.getWorksheets().get(1).getCells().get("A1").setValue("DEMO TEXT ON SHEET2"); | |
// Set currently active sheet index to 1 i.e. Sheet2 | |
workbook.getWorksheets().setActiveSheetIndex(1); | |
// Save workbook to SVG. It shall render the active sheet only to SVG | |
workbook.save(outputDir + "ConvertActiveWorksheetToSVG_out.svg"); |