Managing Pictures
Aspose.Cells allows developers to add pictures to spreadsheets at runtime. Moreover, the positioning of these pictures can be controlled at runtime, which is discussed in more detail in the coming sections.
This article explains how to add pictures, and how to insert an image that shows the content of certain cells.
Adding Pictures
Adding pictures to a spreadsheet is very easy. It only takes a few lines of code: Simply call the Add method of the Pictures collection (encapsulated in the Worksheet object). The Add method takes the following parameters:
- Upper left row index, the index of the upper left row.
- Upper left column index, the index of the upper left column.
- Image file name, the name of the image file, complete with path.
// 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"); |
Positioning Pictures
There are two possible ways to control the positioning of pictures using Aspose.Cells:
- Proportional positioning: define a position proportional to the row height and width.
- Absolute positioning: define the exact position on the page where the image will be inserted, for example, 40 pixels to the left and 20pixels below the edge of the cell.
Proportional Positioning
Developers can position the pictures proportional to row height and column width using the UpperDeltaX and UpperDeltaY properties of the Aspose.Cells.Drawing.Picture object. A Picture object can be obtained from the Pictures collection by passing its picture index. This example places an image in the F6 cell.
// 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"); |
Absolute Positioning
Developers can also position the pictures absolutely by using the Left and Top properties of the Picture object. This example places an image in cell F6, 60 pixels from the left and 10 pixels from the top of the cell.
// 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"); |
Inserting a Picture Based on Cell Reference
Aspose.Cells lets you display the contents of a worksheet cell in an image shape. You can link the picture to the cell that contains the data that you want to display. Since the cell, or cell range, is linked to the graphic object, changes that you make to the data in that cell or cell range automatically appear in the graphic object.
Add a picture to the worksheet by calling the AddPicture method of the ShapeCollection collection (encapsulated in the Worksheet object). Specify the cell range by using the Formula attribute of the Picture object.
// 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"); |
Advance topics
- Add Conditional Icons Set with the Cell Text
- Insert a Linked Picture from Web Address
- Insert a Picture Based on Cell Reference
- Load a Web Image from a URL into an Excel Worksheet