插入 Excel 文件的图片和形状。
有时您需要在工作表中插入一些必要的形状。您可能需要在工作表的不同位置插入相同的形状。或者您需要在工作表中批量插入形状。
不要担心!Aspose.Cells支持所有这些操作。
excel中的形状主要分为以下几种:
- 图片
- 对象
- 线条
- 矩形
- 基本形状
- 块箭头
- 方程形状
- 流程图
- 星星和横幅
- 标注
本指南文档将从每种类型中选择一个或两个形状来制作示例。通过这些示例,您将学习如何使用Aspose.Cells将指定的形状插入到工作表中。
Excel工作表中添加图片C#
将图片添加到电子表格非常容易。它只需要几行代码: 只需调用添加的方法图片集合(封装在工作表目的)。这添加方法采用以下参数:
- 左上行索引左上行的索引。
- 左上列索引左上列的索引。
- 图像文件名,图像文件的名称,完整的路径。
// 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# 中将 OLE 对象插入 Excel 工作表
Aspose.Cells 支持在工作表中添加、提取和操作 OLE 对象。因此,Aspose.Cells 具有OleObject集合类,用于将新的 OLE 对象添加到集合列表中。另一个班级,对象代表一个 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
您可以使用以下方法在工作表中插入一行。
以下示例显示如何向工作表插入行。
// 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 工作表插入直线箭头
线箭头的形状属于线条category.It 是 line 的特例。
在 Microsoft Excel 中(例如 2007):
- 选择要插入直线箭头的单元格
- 单击插入菜单,然后单击形状。
- 然后,从“最近使用的形状”或“线条”中选择线条箭头
使用 Aspose.Cells
您可以使用以下方法在工作表中插入线箭头。
以下示例显示如何将线箭头插入工作表。
// 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
您可以使用以下方法在工作表中插入一个矩形。
以下示例显示如何将矩形插入到工作表中。
// 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
您可以使用以下方法在工作表中插入一个立方体。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将多维数据集插入工作表。
// 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
您可以使用以下方法在工作表中插入标注四箭头。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将标注四箭头插入工作表。
// 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
您可以使用以下方法在工作表中插入乘号。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将乘号插入工作表。
// 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
您可以使用以下方法在工作表中插入多文档。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将多文档插入到工作表中。
// 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
您可以使用以下方法在工作表中插入一个五角星。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将五角星插入工作表。
// 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
您可以使用以下方法在工作表中插入思想泡泡云。
公共形状 AddAutoShape( 自选图形类型, int upperLeftRow, 国际顶级, int upperLeftColumn, 剩下的, 整数高度, 整数宽度 )
该方法返回一个形状目的。
以下示例显示如何将思想泡泡云插入到工作表中。
// 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); |
执行上面的代码,你会得到如下结果: