向工作表添加图标

在Aspose.Cells中向工作表添加图标

如果您需要使用Aspose.Cells在Excel文件中添加’图标',那么本文档可以为您提供一些帮助。

对应于插入图标操作的Excel界面如下:

  • 选择要插入到工作表中的图标的位置
  • 左键单击插入->图标
  • 在打开的窗口中,选择上图中红色矩形所示的图标
  • 单击 插入,将其插入到 Excel 文件中。

效果如下:

这里,我们已经准备了样本代码,以帮助您使用Aspose.Cells插入图标。还有一个必要的样本文件和一个图标资源文件。我们使用Excel界面在样本文件中插入一个与资源文件相同的显示效果的图标。

C#

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
//Read icon resource file data
string fileName = "icon.svg";
FileStream fsSource = File.OpenRead(fileName);
byte[] bytes = new byte[fsSource.Length];
int numBytesToRead = (int)fsSource.Length;
int numBytesRead = 0;
while (numBytesToRead > 0)
{
// Read may return anything from 0 to numBytesToRead.
int n = fsSource.Read(bytes, numBytesRead, numBytesToRead);
// Break when the end of the file is reached.
if (n == 0)
break;
numBytesRead += n;
numBytesToRead -= n;
}
fsSource.Close();
// Create workbook from sample file
Workbook workbook = new Workbook("sample.xlsx");
// Access first worksheet from the collection
Worksheet sheet = workbook.Worksheets[0];
// Add the icon to the worksheet
sheet.Shapes.AddIcons(3, 0, 7, 0, 100, 100, bytes, null);
//Set a prompt message
Cell c = sheet.Cells[8,7];
c.Value = "Insert via Aspose.Cells";
Style s = c.GetStyle();
s.Font.Color = Color.Blue;
c.SetStyle(s);
//Save.You can check your icon in this way.
workbook.Save("sample2.xlsx", SaveFormat.Xlsx);

当您在项目中执行上述代码时,将获得以下结果: