导出工作表中的单元格范围为图像
Contents
[
Hide
]
可能的使用场景
您可以使用 Aspose.Cells 制作工作表的图像。但是,有时您只需将工作表中的一定范围的单元格导出为图像。本文解释了如何实现这一点。
导出工作表中的单元格范围为图像
要对一个范围进行图像化处理,将打印区域设置为所需的范围,然后将所有边距设置为 0。还需将 ImageOrPrintOptions.OnePagePerSheet 设置为 true。以下代码将图像化范围 D8:G16。以下是 示例 Excel 文件 的屏幕截图,以及代码中使用的图像。您可以使用任何 Excel 文件尝试此代码。
示例 Excel 文件及其导出图像的屏幕截图
执行该代码仅创建范围 D8:G16 的图像。
示例代码
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-.NET | |
//Source directory | |
string sourceDir = RunExamples.Get_SourceDirectory(); | |
//Output directory | |
string outputDir = RunExamples.Get_OutputDirectory(); | |
// Create workbook from source file. | |
Workbook workbook = new Workbook(sourceDir + "sampleExportRangeOfCellsInWorksheetToImage.xlsx"); | |
// Access the first worksheet | |
Worksheet worksheet = workbook.Worksheets[0]; | |
// Set the print area with your desired range | |
worksheet.PageSetup.PrintArea = "D8:G16"; | |
// Set all margins as 0 | |
worksheet.PageSetup.LeftMargin = 0; | |
worksheet.PageSetup.RightMargin = 0; | |
worksheet.PageSetup.TopMargin = 0; | |
worksheet.PageSetup.BottomMargin = 0; | |
// Set OnePagePerSheet option as true | |
ImageOrPrintOptions options = new ImageOrPrintOptions(); | |
options.OnePagePerSheet = true; | |
options.ImageType = ImageType.Jpeg; | |
options.HorizontalResolution = 200; | |
options.VerticalResolution = 200; | |
// Take the image of your worksheet | |
SheetRender sr = new SheetRender(worksheet, options); | |
sr.ToImage(0, outputDir + "outputExportRangeOfCellsInWorksheetToImage.jpg"); |