カスタムXMLパーツの追加およびIDでの選択

可能な使用シナリオ

カスタムXMLパーツは、Microsoft Excelドキュメント内に格納されるXMLデータであり、それを処理するアプリケーションで使用されます。現時点では、Microsoft Excel UIで直接追加する方法はありませんが、VSTO、Aspose.Cellsなどを使用してプログラムで追加できます。Aspose.Cells APIを使用してカスタムXMLパーツを追加したい場合はWorkbook.CustomXmlParts.Add()メソッドを使用してください。また、CustomXmlPart.IDプロパティを使用してそのIDを設定できます。同様に、IDでカスタムXMLパーツを選択したい場合はWorkbook.CustomXmlParts.SelectByID()メソッドを使用できます。

カスタムXMLパーツの追加およびIDでの選択

以下のサンプルコードは、まずWorkbook.CustomXmlParts.Add()メソッドを使用して4つのカスタムXMLパーツを追加します。次にCustomXmlPart.IDプロパティを使用してそれらのIDを設定します。最後にWorkbook.CustomXmlParts.SelectByID()メソッドを使用して追加されたカスタムXMLパーツの1つを検索または選択します。以下のコードのコンソール出力も参照してください。

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-.NET
// Create empty workbook.
Workbook wb = new Workbook();
// Some data in the form of byte array.
// Please use correct XML and Schema instead.
byte[] btsData = new byte[] { 1, 2, 3 };
byte[] btsSchema = new byte[] { 1, 2, 3 };
// Create four custom xml parts.
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);
// Assign ids to custom xml parts.
wb.CustomXmlParts[0].ID = "Fruit";
wb.CustomXmlParts[1].ID = "Color";
wb.CustomXmlParts[2].ID = "Sport";
wb.CustomXmlParts[3].ID = "Shape";
// Specify search custom xml part id.
String srchID = "Fruit";
srchID = "Color";
srchID = "Sport";
// Search custom xml part by the search id.
Aspose.Cells.Markup.CustomXmlPart cxp = wb.CustomXmlParts.SelectByID(srchID);
// Print the found or not found message on console.
if (cxp == null)
{
Console.WriteLine("Not Found: CustomXmlPart ID " + srchID);
}
else
{
Console.WriteLine("Found: CustomXmlPart ID " + srchID);
}
Console.WriteLine("AddCustomXMLPartsAndSelectThemByID executed successfully.");

コンソール出力

 Found: CustomXmlPart ID Sport