Convert XML to Excel CSharp

XML (Extensible Markup Language) is similar to HTML but uses different tags for defining objects. It is used to store and transport data without being dependent on software or hardware tools. In certain cases, we may need to export data from XML to Excel (XLSX or XLS) for further processing. Excel files are widely used to store and organize data. In this article, we will learn how to convert XML to Excel using C#.

The following topics shall be covered in this article:

  1. C# XML to Excel Converter API
  2. Convert XML to Excel
  3. Export Specific Columns from XML to Excel

C# XML to Excel Converter API

For converting XML to Excel, we will be using the Aspose.Cells for .NET API. It is a feature-rich and powerful API that allows performing Excel automation features programmatically without needing a Microsoft Excel application.

The Workbook class of the API represents an Excel spreadsheet. It allows the opening and saving of native Excel files. It also provides methods to copy data from other Workbooks, combine two Workbooks, and protect the Excel spreadsheet. This class allows importing XML data files into the workbook using the ImportXml() method. The save() method of this class allows saving the workbooks in the specified formats.

Please either download the DLL of the API or install it using NuGet.

PM> Install-Package Aspose.Cells

Convert XML to Excel in C#

We can easily convert an XML data file into Excel workbook by following the steps given below:

  1. Create an instance of the Workbook class.
  2. Call the ImportXml() method to import XML file.
  3. Save the Workbook as an XLSX file using the Save() method. It takes the output file path as an argument.

The code example given below shows how to convert XML to Excel using C#.

Convert XML to Excel in C#

Export Specific Columns from XML to Excel in C#

We need to create an XSLT file with an HTML layout to render our data in a table. An XLS file is a style sheet that can be used to transform XML documents into other document types and to format the output. We will include only the required columns in the XSLT file for converting into Excel. The XLS file that will help us do this might look something like this:

We can get specific columns from an XML data file into an Excel workbook by following the steps given below:

  1. Firstly, read the XML file using the File.ReadAllText() method.
  2. Next, read the XSLT file using the File.ReadAllText() method.
  3. Then, transform XML content using the XslCompiledTransform class instance.
  4. After that, specify various worksheet settings such as AutoFitRows, and AutoFitColumns.
  5. Save the Workbook as an XLSX file using the Save() method. It takes the output file path as an argument.

The code example given below shows how to convert specific columns from XML to Excel using C#.

Convert XML to Excel in C#

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to import data from an XML file to Excel in C#. We have also seen how to extract specific column data from an XML file and convert it to Excel programmatically. Besides, you can learn more about Aspose.Cells for .NET API using the documentation. In case of any ambiguity, please feel free to contact us on the forum.

See Also