管理图片
Aspose.Cells允许开发人员在运行时向电子表格添加图片。此外,可以在运行时控制这些图片的定位,更多细节将在接下来的章节中讨论。
本文章介绍了如何添加图片,以及如何插入显示特定单元格内容的图片。
添加图片
向电子表格中添加图片非常简单。只需几行代码: 只需调用Worksheet中封装的Pictures集合的Add方法。Add 方法接受以下参数:
- 左上角行索引,左上角行的索引。
- 左上角列索引,左上角列的索引。
- 图像文件名,图像文件的名称,包括完整路径。
// 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"); |
定位图片
使用Aspose.Cells控制图片定位有两种可能的方法:
- 比例定位:定义相对于行高和列宽的位置。
- 绝对定位:定义图片将插入到页面上的确切位置,例如,距离单元格左侧40像素,距离边缘下方20像素。
比例定位
开发人员可以使用Aspose.Cells.Drawing.Picture对象的UpperDeltaX和UpperDeltaY属性将图片定位于行高和列宽的比例。可以通过传递其图片索引从Pictures集合获取Picture对象。此示例将图像放置在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"); |
绝对定位
开发人员还可以使用Picture对象的Left和Top属性绝对定位图片。此示例将图像放置在F6单元格中,离左侧60像素,离顶部10像素。
// 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"); |
基于单元格引用插入图片
Aspose.Cells允许您在图像形状中显示工作表单元格的内容。您可以将图片链接到包含您要显示数据的单元格。由于单元格或单元格范围与图形对象链接,因此您对该单元格或单元格范围中的数据所做的更改将自动显示在图形对象中。
通过调用ShapeCollection对象中封装的AddPicture集合的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"); |