Gestione delle immagini

Aspose.Cells consente ai programmatori di aggiungere immagini ai fogli di calcolo in fase di esecuzione. Inoltre, la posizione di queste immagini può essere controllata in fase di esecuzione, come discusso più in dettaglio nelle sezioni successive.

Questo articolo spiega come aggiungere immagini e come inserire un’immagine che mostra il contenuto di determinate celle.

Aggiunta di immagini

Aggiungere immagini a un foglio di calcolo è molto semplice. Bastano poche righe di codice.

Chiamare semplicemente il metodo add della collezione Pictures (incapsulata nell’oggetto Worksheet). Il metodo add accetta i seguenti parametri:

  • Indice della riga in alto a sinistra, l’indice della riga in alto a sinistra.
  • Indice della colonna in alto a sinistra, l’indice della colonna in alto a sinistra.
  • Nome del file immagine, il nome del file immagine, completo di percorso.
// 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.getDataDir(AddingPictures.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
WorksheetCollection worksheets = workbook.getWorksheets();
// Obtaining the reference of first worksheet
Worksheet sheet = worksheets.get(0);
// Adding a picture at the location of a cell whose row and column indices are 5 in the worksheet. It is "F6" cell
int pictureIndex = sheet.getPictures().add(5, 5, dataDir + "logo.jpg");
Picture picture = sheet.getPictures().get(pictureIndex);
// Saving the Excel file
workbook.save(dataDir + "book1.xls");

Posizionamento delle immagini

Le immagini possono essere posizionate utilizzando Aspose.Cells come segue:

Posizionamento Assoluto

I programmatori possono posizionare le immagini in modo assoluto utilizzando i metodi setUpperDeltaX e setUpperDeltaY dell’oggetto Picture.

// 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.getDataDir(AbsolutePositioning.class);
// Instantiating a Workbook object
Workbook workbook = new Workbook();
// Obtaining the reference of the newly added worksheet.
int sheetIndex = workbook.getWorksheets().add();
Worksheet worksheet = workbook.getWorksheets().get(sheetIndex);
// Adding a picture at the location of a cell whose row and column indices are 5 in the worksheet. It is "F6" cell
int pictureIndex = worksheet.getPictures().add(5, 5, dataDir + "logo.jpg");
Picture picture = worksheet.getPictures().get(pictureIndex);
// Positioning the picture proportional to row height and colum width
picture.setUpperDeltaX(200);
picture.setUpperDeltaY(200);
// Saving the Excel file
workbook.save(dataDir + "test_pictures.xls");

Argomenti avanzati