Esporta un intervallo di celle in un foglio di lavoro in un immagine

Possibili Scenari di Utilizzo

Puoi creare un’immagine di un foglio di lavoro usando Aspose.Cells per Python via .NET. Tuttavia, a volte è necessario esportare solo un intervallo di celle in un’immagine. Questo articolo spiega come farlo.

Esportare un intervallo di celle in un foglio di lavoro in un’immagine

Per prendere un’immagine di un intervallo, impostare l’area di stampa sull’intervallo desiderato e quindi impostare tutti i margini a 0. Imposta anche ImageOrPrintOptions.one_page_per_sheet su true. Il seguente codice prende un’immagine dell’intervallo D8:G16. Di seguito è riportata un’istantanea del file di Excel di esempio usato nel codice. Puoi provare il codice con qualsiasi file di Excel.

Screenshot del file di Excel di esempio e la sua immagine esportata

todo:image_alt_text

Eseguendo il codice viene creata un’immagine dell’intervallo D8:G16 soltanto.

todo:image_alt_text

Codice di Esempio

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")