在工作表中管理图片

添加图片

使用Aspose.Cells.GridDesktop向单元格添加超链接,请按以下步骤操作:

  • 向您的表单中添加Aspose.Cells.GridDesktop控件
  • 访问任何所需的工作表
  • 通过指定图片的文件路径和将插入图片的单元格名称向工作表添加图片

工作表对象中的图片集合提供了多载入的添加方法。开发人员可以根据特定需求使用任何多载入版本的添加方法。使用这些多载入版本的添加方法,可以从文件、流或Image对象添加图片。

以下是向工作表添加图片的示例代码。

// 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();
}

访问图片

要访问和修改工作表中的现有图片,开发人员可以通过指定图片插入的单元格(使用单元格名称或行和列号来指定位置)简单地从工作表图片集合中访问图片。一旦访问图片,开发人员可以在运行时修改其图像。

以下是访问和修改工作表中图片的示例代码。

// 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");

移除图片

要删除现有图片,开发人员可以简单地访问所需的工作表,然后通过指定包含图片的单元格(使用其名称或行和列号)来从工作表图片集合中移除图片。

下面的代码显示了如何从工作表中移除图片。

// 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);