Add Custom XML Parts and Select them by ID

Possible Usage Scenarios

Custom XML Parts are the XML data that is stored inside the Microsoft Excel documents and are used by the applications that deal with them. There is no direct way of adding them using Microsoft Excel UI at the moment. However, you can add them programmatically in various ways e.g. using VSTO, using Aspose.Cells etc. Please use Workbook.getCustomXmlParts().add() method if you want to add Custom XML Part using Aspose.Cells API. You can also set its ID, using the CustomXmlPart.ID property. Similarly, if you want to select Custom XML Part by ID, you can use Workbook.getCustomXmlParts().selectByID() method.

Add Custom XML Parts and Select them by ID

The following sample code first adds four Custom XML Parts using Workbook.getCustomXmlParts().add() method. It then set their IDs using CustomXmlPart.ID property. Finally, it finds or selects one of the added Custom XML Part using Workbook.getCustomXmlParts().selectByID() method. Please also see the console output of the code given below for a reference.

Sample Code

// 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);
}

Console Output

Found: CustomXmlPart ID Sport