Esporta un intervallo di celle in un foglio di lavoro in un immagine
Possibili Scenari di Utilizzo
È possibile creare un’immagine di un foglio di lavoro utilizzando Aspose.Cells. Tuttavia, talvolta è necessario esportare solo un intervallo di celle in un foglio di lavoro in un’immagine. Questo articolo spiega come raggiungere questo obiettivo.
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.OnePagePerSheet 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
Eseguendo il codice viene creata un’immagine dell’intervallo D8:G16 soltanto.
Codice di Esempio
// 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"); |