导出工作表中的单元格范围为图像

可能的使用场景

你可以使用 Aspose.Cells for Python via .NET 将工作表制作成图片。但有时你只需要导出工作表的一部分单元格为图像。本文介绍了实现方法。

导出工作表中的单元格范围为图像

要对一个范围进行图像化处理,将打印区域设置为所需的范围,然后将所有边距设置为 0。还需将 ImageOrPrintOptions.one_page_per_sheet 设置为 true。以下代码将图像化范围 D8:G16。以下是 示例 Excel 文件 的屏幕截图,以及代码中使用的图像。您可以使用任何 Excel 文件尝试此代码。

示例 Excel 文件及其导出图像的屏幕截图

todo:image_alt_text

执行该代码仅创建范围 D8:G16 的图像。

todo:image_alt_text

示例代码

from aspose.cells import Workbook
from aspose.cells.drawing import ImageType
from aspose.cells.rendering import ImageOrPrintOptions, SheetRender
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET
# Source directory
sourceDir = RunExamples.Get_SourceDirectory()
# Output directory
outputDir = RunExamples.Get_OutputDirectory()
# Create workbook from source file.
workbook = Workbook(sourceDir + "sampleExportRangeOfCellsInWorksheetToImage.xlsx")
# Access the first worksheet
worksheet = workbook.worksheets[0]
# Set the print area with your desired range
worksheet.page_setup.print_area = "D8:G16"
# Set all margins as 0
worksheet.page_setup.left_margin = 0.0
worksheet.page_setup.right_margin = 0.0
worksheet.page_setup.top_margin = 0.0
worksheet.page_setup.bottom_margin = 0.0
# Set OnePagePerSheet option as true
options = ImageOrPrintOptions()
options.one_page_per_sheet = True
options.image_type = ImageType.JPEG
options.horizontal_resolution = 200
options.vertical_resolution = 200
# Take the image of your worksheet
sr = SheetRender(worksheet, options)
sr.to_image(0, outputDir + "outputExportRangeOfCellsInWorksheetToImage.jpg")