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 facile. Bastano poche righe di codice: Basta chiamare 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-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[sheetIndex]; | |
// Adding a picture at the location of a cell whose row and column indices | |
// Are 5 in the worksheet. It is "F6" cell | |
worksheet.Pictures.Add(5, 5, dataDir + "logo.jpg"); | |
// Saving the Excel file | |
workbook.Save(dataDir + "output.xls"); |
Posizionamento delle immagini
Ci sono due possibili modi per controllare il posizionamento delle immagini utilizzando Aspose.Cells:
- Posizionamento proporzionale: definire una posizione proporzionale all’altezza e alla larghezza della riga.
- Posizionamento assoluto: definire l’esatta posizione sulla pagina in cui l’immagine sarà inserita, ad esempio, 40 pixel a sinistra e 20 pixel sotto il bordo della cella.
Posizionamento proporzionale
Gli sviluppatori possono posizionare le immagini proporzionalmente all’altezza della riga e alla larghezza della colonna utilizzando le proprietà UpperDeltaX e UpperDeltaY dell’oggetto Aspose.Cells.Drawing.Picture. Un oggetto Picture può essere ottenuto dalla raccolta Pictures passando l’indice dell’immagine. Questo esempio posiziona un’immagine nella cella F6.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Create directory if it is not already present. | |
bool IsExists = System.IO.Directory.Exists(dataDir); | |
if (!IsExists) | |
System.IO.Directory.CreateDirectory(dataDir); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[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.Pictures.Add(5, 5, dataDir + "logo.jpg"); | |
// Accessing the newly added picture | |
Aspose.Cells.Drawing.Picture picture = worksheet.Pictures[pictureIndex]; | |
// Positioning the picture proportional to row height and colum width | |
picture.UpperDeltaX = 200; | |
picture.UpperDeltaY = 200; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Posizionamento Assoluto
Gli sviluppatori possono anche posizionare le immagini in modo assoluto utilizzando le proprietà Left e Top dell’oggetto Picture. Questo esempio posiziona un’immagine nella cella F6, 60 pixel a sinistra e 10 pixel dall’alto della cella.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(); | |
// Adding a new worksheet to the Workbook object | |
int sheetIndex = workbook.Worksheets.Add(); | |
// Obtaining the reference of the newly added worksheet by passing its sheet index | |
Worksheet worksheet = workbook.Worksheets[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.Pictures.Add(5, 5, dataDir + "logo.jpg"); | |
// Accessing the newly added picture | |
Aspose.Cells.Drawing.Picture picture = worksheet.Pictures[pictureIndex]; | |
// Absolute positioning of the picture in unit of pixels | |
picture.Left = 60; | |
picture.Top = 10; | |
// Saving the Excel file | |
workbook.Save(dataDir + "book1.out.xls"); |
Inserimento di un’immagine in base al riferimento della cella
Aspose.Cells consente di visualizzare i contenuti di una cella del foglio di lavoro in una forma di immagine. È possibile collegare l’immagine alla cella che contiene i dati che si desidera visualizzare. Poiché la cella, o il range di celle, è collegata all’oggetto grafico, le modifiche apportate ai dati in quella cella o in quel range di celle appaiono automaticamente nell’oggetto grafico.
Aggiungi un’immagine al foglio di lavoro chiamando il metodo AddPicture della raccolta ShapeCollection (incapsulata nell’oggetto Worksheet). Specifica il range di celle utilizzando l’attributo Formula dell’oggetto Picture.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// The path to the documents directory. | |
string dataDir = RunExamples.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Instantiate a new Workbook | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet's cells collection | |
Cells cells = workbook.Worksheets[0].Cells; | |
// Add string values to the cells | |
cells["A1"].PutValue("A1"); | |
cells["C10"].PutValue("C10"); | |
// Add a blank picture to the D1 cell | |
Picture pic = workbook.Worksheets[0].Shapes.AddPicture(0, 3, 10, 6, null); | |
// Specify the formula that refers to the source range of cells | |
pic.Formula = "A1:C10"; | |
// Update the shapes selected value in the worksheet | |
workbook.Worksheets[0].Shapes.UpdateSelectedValue(); | |
// Save the Excel file. | |
workbook.Save(dataDir + "output.out.xls"); |