管理工作表中的图片
Contents
[
Hide
]
大多数人认为图片比文字更能说明问题。这就是为什么Aspose.Cells.GridDesktop支持在工作表中添加图片来执行人们的这一信念。在本主题中,我们将讨论在工作表中添加和操作图片。
添加图片
要使用 Aspose.Cells.GridDesktop 添加到单元格的超链接,请按照以下步骤操作:
- 将 Aspose.Cells.GridDesktop 控件添加到您的形式
- 访问任何想要的工作表
- 添加图片通过指定图片的文件路径和将插入图片的单元格名称到工作表
图片收集在工作表对象提供重载添加方法。开发人员可以使用任何重载版本添加方法根据自己的具体需要。使用这些重载版本添加方法,可以从文件、流或图片目的。
下面是将图片添加到工作表中的示例代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.GetActiveWorksheet(); | |
// Adding picture to "b2" cell from file | |
sheet.Pictures.Add("b2", dataDir + "AsposeGrid.jpg"); | |
// Creating a stream contain picture | |
FileStream fs = new FileStream(dataDir + "AsposeLogo.jpg", FileMode.Open); | |
try | |
{ | |
// Adding picture to "b3" cell from stream | |
sheet.Pictures.Add(2, 1, fs); | |
} | |
catch (Exception ex) | |
{ | |
MessageBox.Show(ex.Message); | |
} | |
finally | |
{ | |
// Closing stream | |
fs.Close(); | |
} |
访问图片
要访问和修改工作表中的现有图片,开发人员可以从图片的集合工作表通过指定插入图片的单元格(使用单元格名称或其在行号和列号方面的位置)。一旦图片被访问,开发者就可以在运行时修改它的Image。
下面是访问和修改工作表中图片的示例代码。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 = Utils.GetDataDir(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Accessing a picture added to "c3" cell (specified using its row & column number) | |
Aspose.Cells.GridDesktop.Data.GridPicture picture1 = sheet.Pictures[1]; | |
// Modifying the image | |
picture1.Image = Image.FromFile(dataDir + "Aspose.Grid.jpg"); |
删除图片
要删除现有图片,开发人员可以简单地访问所需的工作表,然后去掉图片来自图片的集合工作表通过指定包含图片的单元格(使用其名称或行号和列号)。
在下面的代码中显示了如何从工作表中删除图片。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET | |
// Accessing first worksheet of the Grid | |
Worksheet sheet = gridDesktop1.Worksheets[0]; | |
// Removing picture from "c3" cell | |
sheet.Pictures.Remove(2, 2); |