ワークシートにアイコンを追加する

Aspose.Cells でワークシートにアイコンを追加

Excel ファイルに ‘アイコン’ を追加する必要がある場合は、このドキュメントが役立ちます。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);

プロジェクトで上記のコードを実行すると、次の結果が得られます。