文档半岛外围网上直营>>Aspose.Cells开发者指南>>Aspose.Cells功能演示:在Java中转换Excel文件到图像
Aspose.Cells功能演示:在Java中转换Excel文件到图像
Excel电子表格被广泛用于存储,组织和分析数据。但是,不能将Excel工作簿或工作表直接嵌入到Web或桌面应用程序中。合适的选项之一是将工作表转换为图像或HTML格式。在本文中,将学习如何使用Java将Excel XLSX / XLS文件转换为PNG,JPEG,BMP和其他图像格式。
- 将Excel文件转换为图像
- Excel到图像转换的附加选项
Aspose.Cells for Java是功能强大的电子表格处理API,可将工作表高质量转换为PNG,JPEG,BMP和其他流行的图像格式。点击下方按钮可下载试用。
将Excel XLSX转换为Java中的图像
Aspose.Cells for Java支持将Excel文件转换为以下图像格式:
- EMF
- WMF
- JPEG
- PNG
- BMP
- GIF
- TIFF
- SVG
- GLTF
- PICT
- SVM
- Office Compatible EMF
以下是将工作表转换为PNG图像的API引用步骤。
- 使用工作簿 类加载Excel文件。
- 创建ImageOrPrintOptions类的实例。
- 获取要转换为工作表对象的工作表。
- 创建一个SheetRender对象,并使用Worksheet和ImageOrPrintOptions对象对其进行初始化。
- 转换工作表并使用SheetRender.toImage(int,String)方法保存图像文件。
下面的代码示例演示如何使用Java将Excel XLSX文件转换为图像。
// Load Excel file
Workbook workbook = new Workbook("workbook.xlsx");
// Create an object of ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the output image type
imgOptions.setImageType(ImageType.PNG);
// Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int page = 0; page < sr.getPageCount(); page++) { // Generate an image for the worksheet sr.toImage(page, "image" + page + ".png"); }
Excel File
Converted PNG
Excel到图像转换的附加选项
Aspose.Cells
for
Java还提供了其他选项,以自定义XLSX进行图像转换。例如,您可以指定网格线的样式,每张纸渲染一张图像,等等。该ImageOrPrintOptions类用于设置这些选项。下面的代码示例演示如何在Excel中使用ImageOrPrintOptions类进行图像转换。
// Load Excel file
Workbook workbook = new Workbook("workbook.xlsx");
// Create an object of ImageOrPrintOptions
ImageOrPrintOptions imgOptions = new ImageOrPrintOptions();
// Set the image type
imgOptions.setImageType(ImageType.PNG);
// Set one page per sheet rendering
imgOptions.setOnePagePerSheet(true);
// Set gridline
imgOptions.setGridlineType(GridlineType.DOTTED);
// Get the first worksheet
Worksheet sheet = workbook.getWorksheets().get(0);
// Create a SheetRender object for the target sheet
SheetRender sr = new SheetRender(sheet, imgOptions);
for (int page = 0; page < sr.getPageCount(); page++) {
// Generate an image for the worksheet
sr.toImage(page, "image" + page + ".png");
}
如果您有任何疑问或需求,请随时加入Aspose技术交流群(761297826),我们很高兴为您提供查询和咨询。

QQ交谈
在线咨询

渝公网安备
50010702500608号

客服热线