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

Contents
[ ]

Per acquisire un’immagine di un intervallo, imposta l’area di stampa sull’intervallo desiderato e quindi imposta tutti i margini a 0. Imposta anche ImageOrPrintOptions.setOnePagePerSheet() su true.

Il seguente codice acquisisce un’immagine dell’intervallo E8:H10. Di seguito è riportata un’immagine dello stesso foglio di lavoro utilizzato nel codice. Puoi provare il codice con qualsiasi foglio di lavoro.

File di input

todo:image_alt_text

Eseguendo il codice viene creata un’immagine solo dell’intervallo E8:H10.

Immagine di output

todo:image_alt_text

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// The path to the documents directory.
String dataDir = Utils.getSharedDataDir(ExportRangeofCells.class) + "TechnicalArticles/";
// Create workbook from source file.
Workbook workbook = new Workbook(dataDir + "book1.xlsx");
// Access the first worksheet
Worksheet worksheet = workbook.getWorksheets().get(0);
// Set the print area with your desired range
worksheet.getPageSetup().setPrintArea("E8:H10");
// Set all margins as 0
worksheet.getPageSetup().setLeftMargin(0);
worksheet.getPageSetup().setRightMargin(0);
worksheet.getPageSetup().setTopMargin(0);
worksheet.getPageSetup().setBottomMargin(0);
// Set OnePagePerSheet option as true
ImageOrPrintOptions options = new ImageOrPrintOptions();
options.setOnePagePerSheet(true);
options.setImageType(ImageType.JPEG);
// Take the image of your worksheet
SheetRender sr = new SheetRender(worksheet, options);
sr.toImage(0, dataDir + "ERangeofCells_out.jpg");

Potresti trovare utile anche l’articolo Conversione foglio di lavoro in diversi formati di immagine.