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

可能な使用シナリオ

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

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

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

サンプルコード

// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java
// 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.getCustomXmlParts().add(btsData, btsSchema);
wb.getCustomXmlParts().add(btsData, btsSchema);
wb.getCustomXmlParts().add(btsData, btsSchema);
wb.getCustomXmlParts().add(btsData, btsSchema);
// Assign ids to custom xml parts.
wb.getCustomXmlParts().get(0).setID("Fruit");
wb.getCustomXmlParts().get(1).setID("Color");
wb.getCustomXmlParts().get(2).setID("Sport");
wb.getCustomXmlParts().get(3).setID("Shape");
// Specify search custom xml part id.
String srchID = "Fruit";
srchID = "Color";
srchID = "Sport";
// Search custom xml part by the search id.
CustomXmlPart cxp = wb.getCustomXmlParts().selectByID(srchID);
// Print the found or not found message on console.
if (cxp == null)
{
System.out.println("Not Found: CustomXmlPart ID " + srchID);
}
else
{
System.out.println("Found: CustomXmlPart ID " + srchID);
}

コンソール出力

Found: CustomXmlPart ID Sport