将工作表中的 Cells 范围导出到图像
Contents
[
Hide
]
您可以使用 Aspose.Cells 制作工作表的图像。但是,有时您只需要将工作表中的一系列单元格导出到图像。本文解释了如何实现这一点。
要拍摄某个范围的图像,请将打印区域设置为所需范围,然后将所有边距设置为 0。同时设置ImageOrPrintOptions.setOnePagePerSheet() 方法到真的.
以下代码获取范围 E8:H10 的图像。下面是代码中使用的源工作簿的屏幕截图。您可以使用任何工作簿试用代码。
输入文件
执行代码仅创建 E8:H10 范围的图像。
输出图像
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
您还可以找到这篇文章将工作表转换为不同的图像格式有帮助。