[]
        
(Showing Draft Content)

ICustomXmlPartCollection

Interface ICustomXmlPartCollection

All Superinterfaces:
Iterable<ICustomXmlPart>

public interface ICustomXmlPartCollection extends Iterable<ICustomXmlPart>
Represents a collection of Custom XML parts in a workbook.

Use this collection to add, retrieve, enumerate, or remove workbook-level Custom XML parts. Each part has a generated ID and a raw XML payload. The collection is persisted when the workbook is saved to an OpenXML workbook format such as XLSX or XLSM.


 ICustomXmlPartCollection customXmlParts = workbook.getCustomXmlParts();
 ICustomXmlPart customXmlPart = customXmlParts.add();
 customXmlPart.setData("<payload><value>42</value></payload>"
         .getBytes(java.nio.charset.StandardCharsets.UTF_8));

 String id = customXmlPart.getId();
 ICustomXmlPart partById = customXmlParts.get(id);
 ICustomXmlPart firstPart = customXmlParts.get(0);
 int count = customXmlParts.getCount();
 
  • Method Summary

    Modifier and Type
    Method
    Description
    add()
    Adds a new Custom XML part.
    get(int index)
    Gets the Custom XML part at the specified zero-based index.
    get(String id)
    Gets the Custom XML part with the specified ID.
    int
    Gets the number of Custom XML parts in the collection.
    void
    remove(int index)
    Removes the Custom XML part at the specified zero-based index.
    void
    Removes the Custom XML part with the specified ID.

    Methods inherited from interface java.lang.Iterable

    forEach, iterator, spliterator
  • Method Details

    • add

      Adds a new Custom XML part.

      The new part receives a generated ID. Set the XML payload by using ICustomXmlPart.setData(byte[]) before saving the workbook.

      
       ICustomXmlPart customXmlPart = workbook.getCustomXmlParts().add();
       customXmlPart.setData("<customer id=\"42\"/>"
               .getBytes(java.nio.charset.StandardCharsets.UTF_8));
       String id = customXmlPart.getId();
       
      Returns:
      The newly created Custom XML part.
    • get

      Gets the Custom XML part with the specified ID.

      The ID is the value returned by ICustomXmlPart.getId(). This lookup is useful after a workbook is reopened or when multiple Custom XML parts are stored in the same workbook.

      
       ICustomXmlPart customXmlPart = workbook.getCustomXmlParts().add();
       String id = customXmlPart.getId();
      
       ICustomXmlPart samePart = workbook.getCustomXmlParts().get(id);
       
      Parameters:
      id - The ID of the Custom XML part.
      Returns:
      The Custom XML part with the specified ID, or null if it is not found.
    • get

      ICustomXmlPart get(int index)
      Gets the Custom XML part at the specified zero-based index.

      Use the index to enumerate or access parts in collection order. If the index is outside the valid range, an IndexOutOfBoundsException is thrown.

      
       if (workbook.getCustomXmlParts().getCount() > 0) {
           ICustomXmlPart firstPart = workbook.getCustomXmlParts().get(0);
           byte[] xmlData = firstPart.getData();
       }
       
      Parameters:
      index - The zero-based index of the Custom XML part.
      Returns:
      The Custom XML part at the specified index.
    • remove

      void remove(String id)
      Removes the Custom XML part with the specified ID.

      If no part with the specified ID exists, the collection is not changed.

      
       ICustomXmlPart customXmlPart = workbook.getCustomXmlParts().add();
       String id = customXmlPart.getId();
      
       workbook.getCustomXmlParts().remove(id);
       
      Parameters:
      id - The ID of the Custom XML part to remove.
    • remove

      void remove(int index)
      Removes the Custom XML part at the specified zero-based index.

      If the index is outside the valid range, the collection is not changed.

      
       if (workbook.getCustomXmlParts().getCount() > 0) {
           workbook.getCustomXmlParts().remove(0);
       }
       
      Parameters:
      index - The zero-based index of the Custom XML part to remove.
    • getCount

      int getCount()
      Gets the number of Custom XML parts in the collection.
      
       int count = workbook.getCustomXmlParts().getCount();
       for (int i = 0; i < count; i++) {
           ICustomXmlPart customXmlPart = workbook.getCustomXmlParts().get(i);
       }
       
      Returns:
      The number of Custom XML parts in the collection.