将图标添加到工作表

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

当你在你的项目中执行上面的代码时,你会得到如下结果: