插入Excel文件的图片和形状。
有时候,您需要在工作表中插入一些必要的形状。您可能需要在工作表的不同位置插入相同的形状。或者您需要批量在工作表中插入形状。
不用担心!Aspose.Cells支持所有这些操作。
Excel中的形状主要分为以下类型:
- 图片
- Ole对象
- 线条
- 矩形
- 基本形状
- 方块箭头
- 方程式形状
- 流程图
- 星星和横幅
- 标注
这份指南将从每个类型中选择一个或两个形状来制作示例。通过这些示例,您将学习如何使用Aspose.Cells将指定的形状插入工作表中。
在C#中在Excel工作表中添加图片
向电子表格中添加图片非常简单。只需几行代码: 只需调用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"); |
在C#中向Excel工作表插入OLE对象
Aspose.Cells支持在工作表中添加、提取和操作OLE对象。出于这个原因,Aspose.Cells有OleObjectCollection类,用于向集合列表中添加新OLE对象。另一个类,OleObject,代表一个OLE对象。它有一些重要成员:
- ImageData属性指定图像(图标)数据为字节数组类型。将显示该图像以显示工作表中的OLE对象。
- ObjectData属性指定对象数据的字节数组形式。当您双击OLE对象图标时,将在其关联的程序中显示此数据。
下面的示例演示了如何将一个或多个OLE对象添加到工作表中。
// 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); | |
// Instantiate a new Workbook. | |
Workbook workbook = new Workbook(); | |
// Get the first worksheet. | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Define a string variable to store the image path. | |
string ImageUrl = dataDir + "logo.jpg"; | |
// Get the picture into the streams. | |
FileStream fs = File.OpenRead(ImageUrl); | |
// Define a byte array. | |
byte[] imageData = new Byte[fs.Length]; | |
// Obtain the picture into the array of bytes from streams. | |
fs.Read(imageData, 0, imageData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Get an excel file path in a variable. | |
string path = dataDir + "book1.xls"; | |
// Get the file into the streams. | |
fs = File.OpenRead(path); | |
// Define an array of bytes. | |
byte[] objectData = new Byte[fs.Length]; | |
// Store the file from streams. | |
fs.Read(objectData, 0, objectData.Length); | |
// Close the stream. | |
fs.Close(); | |
// Add an Ole object into the worksheet with the image | |
// Shown in MS Excel. | |
sheet.OleObjects.Add(14, 3, 200, 220, imageData); | |
// Set embedded ole object data. | |
sheet.OleObjects[0].ObjectData = objectData; | |
// Save the excel file | |
workbook.Save(dataDir + "output.out.xls"); |
在C#中向Excel工作表插入线条
线形状属于线条类别。
在Microsoft Excel中(例如2007年):
- 选择要插入线条的单元格
- 点击“插入”菜单,然后点击“形状”
- 接着,从“最近使用的形状”或“线条”中选择线条
使用Aspose.Cells
您可以使用以下方法在工作表中插入线条。
该方法返回一个LineShape对象。
下面的示例显示如何将线条插入工作表。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the line to the worksheet | |
sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1 | |
//sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);//method 2 | |
//sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);//method 3 | |
//Save.You can check your line in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表插入线箭头
箭头线的形状属于线类别。它是线的特殊情况。
在Microsoft Excel中(例如2007年):
- 选择要插入箭头线的单元格
- 点击“插入”菜单,然后点击“形状”
- 接着,从“最近使用的形状”或“线”中选择箭头线
使用Aspose.Cells
您可以使用以下方法在工作表中插入箭头线。
该方法返回一个LineShape对象。
以下示例显示了如何向工作表插入箭头线。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the line arrow to the worksheet | |
Shape s = sheet.Shapes.AddLine(2, 0, 2, 0, 100, 300);//method 1 | |
//Shape s = sheet.Shapes.AddAutoShape(AutoShapeType.Line, 2, 0, 2, 0, 100, 300);//method 2 | |
//Shape s = sheet.Shapes.AddShape(MsoDrawingType.Line, 2, 0, 2, 0, 100, 300);//method 3 | |
//add a arrow at the line begin | |
s.Line.BeginArrowheadStyle = MsoArrowheadStyle.Arrow;//arrow type | |
s.Line.BeginArrowheadWidth = MsoArrowheadWidth.Wide;//arrow width | |
s.Line.BeginArrowheadLength = MsoArrowheadLength.Short;//arrow length | |
//add a arrow at the line end | |
s.Line.EndArrowheadStyle = MsoArrowheadStyle.ArrowOpen;//arrow type | |
s.Line.EndArrowheadWidth = MsoArrowheadWidth.Narrow;//arrow width | |
s.Line.EndArrowheadLength = MsoArrowheadLength.Long;//arrow length | |
//Save.You can check your arrow in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在 C# 中向 Excel 工作表插入矩形
矩形的形状属于矩形类别。
在Microsoft Excel中(例如2007年):
- 选择要插入矩形的单元格
- 点击“插入”菜单,然后点击“形状”
- 接着,从“最近使用的形状”或“矩形”中选择矩形
使用Aspose.Cells
您可以使用以下方法在工作表中插入矩形。
该方法返回一个RectangleShape对象。
以下示例显示了如何向工作表中插入矩形。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the rectangle to the worksheet | |
sheet.Shapes.AddRectangle(2, 0, 2, 0, 100, 300); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在 C# 中向 Excel 工作表插入立方体
立方体的形状属于基本形状类别。
在Microsoft Excel中(例如2007年):
- 选择要插入立方体的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从基本形状中选择立方体
使用Aspose.Cells
您可以使用以下方法在工作表中插入立方体。
该方法返回一个Shape对象。
以下示例显示了如何向工作表中插入立方体。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the cube to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.Cube, 2, 0, 2, 0, 100, 300); | |
//Save.You can check your cube in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表中插入标注四方箭头
呼叫四向箭头的形状属于块箭头类别。
在Microsoft Excel中(例如2007年):
- 选择要插入标注四箭头的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从块箭头中选择标注四箭头
使用Aspose.Cells
您可以使用以下方法在工作表中插入标注四箭头
该方法返回一个Shape对象。
以下示例显示了如何将标注四箭头插入工作表
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the callout quad arrow to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.QuadArrowCallout, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表插入乘号的形状
乘法符号的形状属于方程形状类别
在Microsoft Excel中(例如2007年):
- 选择要插入乘法符号的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从方程形状中选择乘法符号
使用Aspose.Cells
您可以使用以下方法在工作表中插入乘法符号
该方法返回一个Shape对象。
以下示例显示如何将乘法符号插入工作表。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the multiplication sign to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.MathMultiply, 2, 0, 2, 0, 100, 100); | |
//Save.You can check your multiplication in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表插入多文档的形状
多文档的形状属于流程图类别。
在Microsoft Excel中(例如2007年):
- 选择要插入多文档的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从流程图中选择多文档
使用Aspose.Cells
您可以使用以下方法在工作表中插入多文档。
该方法返回一个Shape对象。
以下示例显示如何向工作表中插入多文档。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the multidocument to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.FlowChartMultidocument, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表插入五角星
五角星的形状属于星形图案类别。
在Microsoft Excel中(例如2007年):
- 选择要插入五角星的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从星形和横幅中选择五角星
使用Aspose.Cells
您可以使用以下方法在工作表中插入五角星
该方法返回一个Shape对象。
以下示例显示了如何向工作表中插入五角星。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the Five-pointed star to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.Star5, 2, 0, 2, 0, 100, 100); | |
//Save.You can check your icon in this way. | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果:
在C#中向Excel工作表插入一个思维气泡云
思维气泡云的形状属于标注类别。
在Microsoft Excel中(例如2007年):
- 选择要插入思维气泡云的单元格
- 点击“插入”菜单,然后点击“形状”
- 然后,从标注中选择思维气泡云
使用Aspose.Cells
您可以使用以下方法在工作表中插入思维气泡云。
该方法返回一个Shape对象。
以下示例演示了如何向工作表中插入思维气泡云。
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Create workbook from sample file | |
Workbook workbook = new Workbook(); | |
// Access first worksheet from the collection | |
Worksheet sheet = workbook.Worksheets[0]; | |
// Add the thought bubble cloud to the worksheet | |
sheet.Shapes.AddAutoShape(AutoShapeType.CloudCallout, 2, 0, 2, 0, 100, 100); | |
//Save | |
workbook.Save("sample.xlsx", SaveFormat.Xlsx); |
执行上述代码,您将获得以下结果: