These source code samples are assembled by their programming language and functions they apply. ByteScout Barcode Suite is the bundle that privides 3 SDK products to generate barcodes (Barcode SDK), read barcodes (Barcode Reaer SDK) and read and write spreadsheets (Spreadsheet SDK). It can be applied to convert xml to xls with spreadsheet sdk using C#.
Want to quickly learn? This fast application programming interfaces of ByteScout Barcode Suite for C# plus the guidelines and the code below will help you quickly learn how to convert xml to xls with spreadsheet sdk. Follow the instructions from scratch to work and copy the C# code. If you want to use these C# sample examples in one or many applications then they can be used easily.
ByteScout Barcode Suite free trial version is available on our website. C# and other programming languages are supported.
On-demand (REST Web API) version:
Web API (on-demand version)
On-premise offline SDK for Windows:
60 Day Free Trial (on-premise)
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");
}
}
}
60 Day Free Trial or Visit ByteScout Barcode Suite Home Page
Explore ByteScout Barcode Suite Documentation
Explore Samples
Sign Up for ByteScout Barcode Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout Barcode Suite Home Page
Explore ByteScout Barcode Suite Documentation
Explore Samples
Sign Up for ByteScout Barcode Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: