ByteScout Spreadsheet SDK - C# - Convert XML to XLS - ByteScout

ByteScout Spreadsheet SDK – C# – Convert XML to XLS

  • Home
  • /
  • Articles
  • /
  • ByteScout Spreadsheet SDK – C# – Convert XML to XLS

How to convert XML to XLS in C# using ByteScout Spreadsheet SDK

The tutorial below will demonstrate how to convert XML to XLS in C#

The documentation is designed to help you to implement the features on your side. ByteScout Spreadsheet SDK is the SDK that can write and read, modify and calculate Excel and CSV spreadsheets. Most popular formulas are supported. You may import or export data to and from CSV, XML, JSON as well as to and from databases, arrays. It can convert XML to XLS in C#.

Fast application programming interfaces of ByteScout Spreadsheet SDK for C# plus the instruction and the code below will help you quickly learn how to convert XML to XLS. Just copy and paste the code into your C# application’s code and follow the instruction. You can use these C# sample examples in one or many applications.

Trial version of ByteScout Spreadsheet SDK can be downloaded for free from our website. It also includes source code samples for C# and other programming languages.

Try ByteScout Spreadsheet SDK today: Get 60 Day Free Trial or sign up for Web API

using System; using System.Collections.Generic; using System.Diagnostics; using System.Drawing; using System.Xml; using Bytescout.Spreadsheet; using Bytescout.Spreadsheet.Constants; namespace ConvertXmlToXlsExample { /// <summary> /// This example demonstrates how to create Excel spreadsheet from some XML data. /// Since your XML file has different structure the example just shows technique of XML data reading // and spreadsheet creation. /// </summary> class Program { static void Main() { // Load XML document XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load(@"sample.xml"); // Read columns information from XML data List<string> columns = new List<string>(); XmlNodeList columnNodeList = xmlDocument.SelectNodes("/Report/Columns/Column"); foreach (XmlNode node in columnNodeList) columns.Add(node.Attributes["Name"].Value); // Read row nodes from XML data XmlNodeList rowNodeList = xmlDocument.SelectNodes("/Report/ReportData"); // Create new spreadsheet Spreadsheet spreadsheet = new Spreadsheet(); spreadsheet.RegistrationName = "demo"; spreadsheet.RegistrationKey = "demo"; // Add worksheet Worksheet worksheet = spreadsheet.Worksheets.Add(); // Add column headers for (int c = 0; c < columns.Count; c++) { worksheet[0, c].Value = columns; worksheet[0, c].FillPattern = PatternStyle.Solid; worksheet[0, c].FillPatternForeColor = Color.LightGray; } int rowIndex = 1; // Add rows foreach (XmlNode rowNode in rowNodeList) { // Get cell values from XML data foreach (XmlNode childNode in rowNode.ChildNodes) { // Get cell info from XML data int columnIndex = columns.IndexOf(childNode.Name); string cellValue = childNode.InnerText; Cell cell = worksheet[rowIndex, columnIndex]; // Set cell text cell.Value = cellValue; // Set cell text alignment cell.AlignmentHorizontal = columnIndex == 0 ? AlignmentHorizontal.Left : AlignmentHorizontal.Right; } // Add the row to the table rowIndex++; } // Fit columns width to cell data for (int c = 0; c < columns.Count; c++) worksheet.Columns.AutoFit(); // Save document to file spreadsheet.SaveAsXLS("result.xls"); // Cleanup spreadsheet.Dispose(); // Open document in Excel Process.Start("result.xls"); } } }

Try ByteScout Spreadsheet SDK today: 60 Day Free Trial (on-premise version) or sign up for Web API (on demand version)



Get 60 Day Free Trial or Visit ByteScout Spreadsheet SDK page

Explore ByteScout Spreadsheet SDK documentation


Sign Up for free Web API key

Explore Web API Documentation