カスタム XML パーツを追加し、ID で選択する

考えられる使用シナリオ

カスタム XML パーツは、Microsoft Excel ドキュメント内に保存され、それらを処理するアプリケーションによって使用される XML データです。現時点では、Microsoft Excel UI を使用してそれらを直接追加する方法はありません。ただし、さまざまな方法でプログラムで追加できます。VSTO、使用Aspose.Cellsなどご利用くださいWorkbook.getCustomXmlParts().add() メソッドを使用して、Aspose.Cells API を使用してカスタム XML 部分を追加する場合。CustomXmlPart.ID財産。同様に、カスタム XML パーツを ID で選択する場合は、次を使用できます。[Workbook.getCustomXmlParts().selectByID()](https://reference.aspose.com/cells/java/com.aspose.cells/customxmlpartcollection#selectByID(java.lang.String)) 方法。

カスタム XML パーツを追加し、ID で選択する

次のサンプル コードは、最初に を使用して 4 つのカスタム XML パーツを追加します。[Workbook.getCustomXmlParts().add()](https://reference.aspose.com/cells/java/com.aspose.cells/customxmlpartcollection#add(java.lang.Object)) 方法。次に、ID を使用して設定します。CustomXmlPart.ID財産。最後に、追加されたカスタム XML 部分の 1 つを検索または選択します。[Workbook.getCustomXmlParts().selectByID()](https://reference.aspose.com/cells/java/com.aspose.cells/customxmlpartcollection#selectByID(java.lang.String)) 方法。以下のコードのコンソール出力も参照してください。

サンプルコード

// 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