ワークシート内のセルの範囲をイメージにエクスポート

可能な使用シナリオ

Aspose.Cells for Python via .NETを使用してワークシートの画像を作成できます。ただし、時にはワークシート内のセルの範囲だけを画像としてエクスポートする必要があります。この記事では、その方法について説明します。

ワークシートのセルの範囲をイメージにエクスポート

範囲のイメージを取得するには、印刷範囲を所定の範囲に設定し、すべての余白を0に設定し、さらにImageOrPrintOptions.one_page_per_sheettrueに設定します。次のコードは、範囲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")