Add Custom XML Parts in Excel Workbooks

Introduction

When it comes to programmatically managing Excel files, Aspose.Cells for .NET is a standout library. One of its exciting features is the ability to integrate custom XML parts into your Excel workbook. This guide will walk you through the process of adding custom XML parts with unique IDs and retrieving them when needed. Let’s get started!

Prerequisites

Before diving into the code, ensure you have the following set up:

  1. Visual Studio: Make sure you have Visual Studio installed on your machine for coding.
  2. Aspose.Cells for .NET: You need to have this library installed. If you haven’t done so, you can download it here.
  3. .NET Framework: Familiarity with the .NET framework and C# will be helpful.

Once you have everything ready, let’s jump into the coding!

Importing Required Packages

To use Aspose.Cells, add the necessary namespaces at the top of your code:

using System;
using Aspose.Cells;

This allows you to access all the functionalities provided by Aspose.Cells.

Step 1: Create an Empty Workbook

Start by creating an instance of the Workbook class, which represents your Excel workbook:

// Create an empty workbook.
Workbook wb = new Workbook();

This initializes a new workbook where you can add your custom XML parts.

Step 2: Prepare Your XML Data and Schema

Next, prepare your XML data and schema as byte arrays. While this example uses placeholder data, you should replace these with your actual XML content.

// Example data in the form of byte arrays.
byte[] btsData = System.Text.Encoding.UTF8.GetBytes("<root><data>Example</data></root>");
byte[] btsSchema = System.Text.Encoding.UTF8.GetBytes("<root><data></data></root>");

Step 3: Add Custom XML Parts

Now, add your custom XML parts to the workbook by calling the Add method on the CustomXmlParts collection:

// Add custom XML parts to the workbook.
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);
wb.CustomXmlParts.Add(btsData, btsSchema);

This code snippet adds four identical custom XML parts. You can customize this as per your requirements.

Step 4: Assign Unique IDs to Custom XML Parts

Assign unique identifiers to each XML part to facilitate easy retrieval later:

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

These meaningful IDs will help you identify the respective parts later.

Step 5: Specify Search IDs for Custom XML Parts

To retrieve a specific XML part, define the ID you’re searching for:

// Specify search custom XML part ID.
string srchID = "Fruit"; // Change this to other IDs as needed

Step 6: Search for Custom XML Parts by ID

Now, search for the custom XML part using the specified ID:

// Search for the custom XML part by the search ID.
CustomXmlPart cxp = wb.CustomXmlParts.SelectByID(srchID);

This line uses SelectByID to find the XML part associated with the specified ID.

Step 7: Check If the Custom XML Part Was Found

Finally, check whether the XML part was found and print an appropriate message:

// Print the found or not found message to the console.
if (cxp == null)
{
    Console.WriteLine("Not Found: CustomXmlPart ID " + srchID);
}
else
{
    Console.WriteLine("Found: CustomXmlPart ID " + srchID);
}
Console.WriteLine("AddCustomXMLPartsAndSelectThemByID executed successfully.");

Congratulations! You’ve successfully added custom XML parts to your workbook and implemented functionality to search for them by their IDs.

Conclusion

In this article, we explored how to add custom XML parts to an Excel workbook using Aspose.Cells for .NET. By following this step-by-step guide, you learned how to create a workbook, add custom XML parts, assign IDs, and retrieve them efficiently. This feature is invaluable for handling dynamic data in Excel files, enhancing the capabilities of your applications.

FAQs

What is Aspose.Cells?

Aspose.Cells is a powerful .NET library that enables developers to create, manipulate, and convert Excel files without requiring Microsoft Excel installation.

Can I use Aspose.Cells for free?

Yes! You can start with a free trial version. Just download it here.

Is it possible to add multiple custom XML parts to a workbook?

Absolutely! You can add as many custom XML parts as needed, each with unique IDs for easy access.

How can I retrieve XML parts if I don’t know the IDs?

If you don’t know the IDs, you can loop through the CustomXmlParts collection to view available parts and their IDs, making identification easier.

Where can I find more resources or support for Aspose.Cells?

You can check out the documentation for detailed guidance, or visit the support forum for community assistance.


This simple line initializes a new workbook where we can add our custom XML parts.

Step 2: Prepare Your XML Data and Schema

Next, you need to prepare some data in the form of a byte array. Although our example uses placeholder data, in a real-world scenario, you’d replace these byte arrays with actual XML data and schema that you want to integrate into your 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 };

Remember, while this example uses simple byte arrays, you’d typically use valid XML and schema here.

Step 3: Add Custom XML Parts

Now it’s time to add your custom XML parts to the workbook. You can do this by calling the Add method on the CustomXmlParts collection of the workbook.

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

This code snippet adds four identical custom XML parts to the workbook. You can customize this as per your requirements.

Step 4: Assign IDs to Custom XML Parts

Now that we have our XML parts added, let’s give each of them a unique identifier. This ID will help us retrieve the XML parts later.

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

In this step, you are assigning meaningful IDs like “Fruit,” “Color,” “Sport,” and “Shape.” This makes it easy to identify and work with the respective parts afterwards.

Step 5: Specify Search ID for Custom XML Part

When you want to retrieve a specific XML part using its ID, you need to define the ID you’re searching for.

// Specify search custom xml part id.
String srchID = "Fruit";
srchID = "Color";
srchID = "Sport";

In a real application, you would likely want to specify each ID dynamically, but for our example, we’re hardcoding a few.

Step 6: Search for Custom XML Part by ID

Now that we have our search IDs, it’s time to look for the custom XML part corresponding to the specified ID.

// Search custom xml part by the search id.
Aspose.Cells.Markup.CustomXmlPart cxp = wb.CustomXmlParts.SelectByID(srchID);

This line leverages SelectByID to attempt to find the XML part we are interested in.

Step 7: Check If the Custom XML Part Was Found

Finally, we need to check whether the XML part was found and print an appropriate message to the console.

// 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.");

You squashed it! By this point, you have not only added custom XML parts to your workbook but also implemented functionality to search for them by their IDs.

Conclusion

In this article, we explored how to add custom XML parts to an Excel workbook using Aspose.Cells for .NET. By following the step-by-step guide, you were able to create a workbook, add custom XML parts, assign IDs, and retrieve them efficiently. This functionality can be incredibly useful when dealing with dynamic data that needs to be handled in Excel files, making your applications smarter and more capable.

FAQ’s

What is Aspose.Cells?

Aspose.Cells is a robust .NET library that allows developers to create, manipulate, and convert Excel files without needing Microsoft Excel installed.

Can I use Aspose.Cells for free?

Yes! You can start with a free trial version. Just download it here.

Is it possible to add multiple custom XML parts to a workbook?

Absolutely! You can add as many custom XML parts as you need, and each can be assigned unique IDs for easy access.

How can I retrieve XML parts if I don’t know the IDs?

If you don’t know the IDs, you can loop through the CustomXmlParts collection to see the available parts and their IDs, making it easier to identify and access them.

Where can I find more resources or support for Aspose.Cells?

You can check out the documentation for detailed guidance, or visit the support forum for community help.