Every ByteScout tool includes simple example C# source codes that you can get here or in the folder with installed ByteScout product. ByteScout Data Extraction Suite is the bundle that includes three SDK tools for data extraction from PDF, scans, images and from spreadsheets: PDF Extractor SDK, Data Extraction SDK, Barcode Reader SDK and you can use it to convert xml to xls with spreadsheet sdk with C#.
These C# code samples for C# guide developers to speed up coding of the application when using ByteScout Data Extraction Suite. IF you want to implement the functionality, just copy and paste this code for C# below into your code editor with your app, compile and run your application. This basic programming language sample code for C# will do the whole work for you to convert xml to xls with spreadsheet sdk.
You can download free trial version of ByteScout Data Extraction Suite from our website to see and try many others source code samples for C#.
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 Data Extraction Suite Home Page
Explore ByteScout Data Extraction Suite Documentation
Explore Samples
Sign Up for ByteScout Data Extraction Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
60 Day Free Trial or Visit ByteScout Data Extraction Suite Home Page
Explore ByteScout Data Extraction Suite Documentation
Explore Samples
Sign Up for ByteScout Data Extraction Suite Online Training
Get Your API Key
Explore Web API Docs
Explore Web API Samples
also available as: