导出工作表中的单元格范围为图像
Contents
[
Hide
]
您可以使用 Aspose.Cells 制作工作表的图像。但是,有时您只需将工作表中的一定范围的单元格导出为图像。本文解释了如何实现这一点。
要获取范围的图像,请将打印区域设置为所需的范围,然后将所有页面边距设置为0。还应设置ImageOrPrintOptions.setOnePagePerSheet()为true。
以下代码获取了范围E8:H10的图像。下面是代码中使用的源工作簿的屏幕截图。您可以尝试任何工作簿中的代码。
输入文件
执行此代码会创建只包含范围E8:H10的图像。
输出图像
This file contains 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"); |
您可能还会发现文章将工作表转换为不同的图像格式有用。