ワークシート内のセルの範囲をイメージにエクスポート
Contents
[
Hide
]
可能な使用シナリオ
Aspose.Cells for Python via .NETを使用してワークシートの画像を作成できます。ただし、時にはワークシート内のセルの範囲だけを画像としてエクスポートする必要があります。この記事では、その方法について説明します。
ワークシートのセルの範囲をイメージにエクスポート
範囲のイメージを取得するには、印刷範囲を所定の範囲に設定し、すべての余白を0に設定し、さらにImageOrPrintOptions.one_page_per_sheet を trueに設定します。次のコードは、範囲D8:G16のイメージを取得します。下のスクリーンショットは、コードで使用されるサンプルExcelファイルのイメージです。任意のExcelファイルでコードを試すことができます。
サンプルExcelファイルのスクリーンショットとそのエクスポートされたイメージ
コードを実行すると、範囲D8:G16のイメージが作成されます。
サンプルコード
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
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") |