Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
Microsoft Excel is a widely used multifunctional spreadsheet application renowned for its effectiveness in organizing, analyzing, and visualizing data. It is a program that works similarly to a grid. Users may enter various types of input, such as text, numbers, dates, and formulas into individual cells, and the data is arranged into rows and columns for easy management. Its robust formula and function calculation capabilities enable users to perform a wide range of logical, statistical, and mathematical tasks.
Excel offers tools for managing and evaluating data as well as for producing graphs and charts that visually represent the data. It facilitates collaborative efforts by allowing several users to edit and share files simultaneously. It can be customized and automated with the use of macros, VBA, and add-ins due to its adaptability to a range of user demands across sectors. Excel Workbook class is used in a variety of fields, including banking, education, research, and business analytics. It is an essential tool for organizing, evaluating, and using data to inform choices. In this article, we are going to learn how to convert Excel to XML in C#.
using IronXL;
class Program
{
static void Main()
{
// Step 2: Install IronXL library
// Step 3: Initialize IronXL object
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Step 4: Create or load Excel file
// Create a new Excel file
WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");
worksheet ["A1"].Value = "Hello";
worksheet ["B1"].Value = "World";
// Or load an existing Excel file
workbook = WorkBook.Load("path_to_excel_file.xlsx");
// Step 5: Convert Excel to XML
XMLSaveOptions saveOptions = new XMLSaveOptions();
workbook.SaveAsXml("path_to_output_xml_file.xml", saveOptions);
// Step 6: Dispose of the IronXL objects
worksheet.Dispose();
workbook.Dispose();
}
}
using IronXL;
class Program
{
static void Main()
{
// Step 2: Install IronXL library
// Step 3: Initialize IronXL object
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
// Step 4: Create or load Excel file
// Create a new Excel file
WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");
worksheet ["A1"].Value = "Hello";
worksheet ["B1"].Value = "World";
// Or load an existing Excel file
workbook = WorkBook.Load("path_to_excel_file.xlsx");
// Step 5: Convert Excel to XML
XMLSaveOptions saveOptions = new XMLSaveOptions();
workbook.SaveAsXml("path_to_output_xml_file.xml", saveOptions);
// Step 6: Dispose of the IronXL objects
worksheet.Dispose();
workbook.Dispose();
}
}
Imports IronXL
Friend Class Program
Shared Sub Main()
' Step 2: Install IronXL library
' Step 3: Initialize IronXL object
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
' Step 4: Create or load Excel file
' Create a new Excel file
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")
worksheet ("A1").Value = "Hello"
worksheet ("B1").Value = "World"
' Or load an existing Excel file
workbook = WorkBook.Load("path_to_excel_file.xlsx")
' Step 5: Convert Excel to XML
Dim saveOptions As New XMLSaveOptions()
workbook.SaveAsXml("path_to_output_xml_file.xml", saveOptions)
' Step 6: Dispose of the IronXL objects
worksheet.Dispose()
workbook.Dispose()
End Sub
End Class
In the above code, we first install the IronXL library. Then, we initialize the IronXL object and create or load the Excel file. Finally, we use the SaveAsXml
method with the XMLSaveOptions
class to convert the loaded file into XML. Don't forget to dispose of the IronXL objects after use.
A powerful .NET framework Excel library called IronXL was created to facilitate dealing with Excel written in C#, VB.NET, and other .NET languages. It is compatible with the XLS and XLSX file formats. With the use of this library, developers may write, read, edit, and produce Excel spreadsheets more quickly and simply. A vast array of tools and functions are also available.
The key features and properties of IronXL include:
Finance, data analysis, reporting, business intelligence, and software development are just a few of the several sectors that utilize IronXL. Combining data manipulation with Excel integration enables programmers to work with Excel files and produce reliable solutions. To learn more, go to this link.
Setting up a Visual Studio Console project is easy. Follow these steps to create a Console application:
Create a new project: Select File > New > Project.
Provide a name for your project in the "Name" field.
Click "Create" to start working on your new Console application project.
To install the IronXL library, follow these steps:
Install the IronXL library as it is required for the next steps. Use the following command in the NuGet Package Manager Console:
Install-Package IronXL
Another approach is to use the NuGet Package Manager to search for the package "IronXL". Choose the appropriate NuGet package for IronXL from the search results.
You can save data from an Excel file into an XML file format using IronXL's SaveAsXml
function.
Here's an example of how to use the SaveAsXml
function in C# with IronXL:
using IronXL;
class Program
{
static void Main(string [] args)
{
string excelFilePath = "Excel file here";
string xmlFilePath = "XML file here";
// Load Excel file using IronXL
WorkBook workbook = WorkBook.Load(excelFilePath);
// Save Excel data as XML
workbook.SaveAsXml(xmlFilePath);
Console.WriteLine("Excel data saved as XML successfully at: " + xmlFilePath);
}
}
using IronXL;
class Program
{
static void Main(string [] args)
{
string excelFilePath = "Excel file here";
string xmlFilePath = "XML file here";
// Load Excel file using IronXL
WorkBook workbook = WorkBook.Load(excelFilePath);
// Save Excel data as XML
workbook.SaveAsXml(xmlFilePath);
Console.WriteLine("Excel data saved as XML successfully at: " + xmlFilePath);
}
}
Imports IronXL
Friend Class Program
Shared Sub Main(ByVal args() As String)
Dim excelFilePath As String = "Excel file here"
Dim xmlFilePath As String = "XML file here"
' Load Excel file using IronXL
Dim workbook As WorkBook = WorkBook.Load(excelFilePath)
' Save Excel data as XML
workbook.SaveAsXml(xmlFilePath)
Console.WriteLine("Excel data saved as XML successfully at: " & xmlFilePath)
End Sub
End Class
To save the resulting XML file, replace "XML file here" with the appropriate location and "Excel file here" with the path to your actual Excel file.
This code sample imports an Excel file and uses the SaveAsXml
function provided by IronXL to convert the Excel file data into XML format without manually looping through rows and columns to structure the XML. This method internally converts the Excel data to XML format and then publishes it to the specified XML file.
Make sure you have the necessary permissions to write to the directory when saving the XML file. The content and layout of the original Excel file may influence the type and structure of the resulting XML.
Input file:
Result:
To learn more about the code, refer to the code example here.
An easy and efficient way to export data from an Excel file to XML format is by using IronXL's SaveAsXml
function. This method eliminates the need for manual iteration through rows and cells when converting Excel data into XML format. IronXL is a .NET library that provides this functionality.
By utilizing IronXL's SaveAsXml
function in C#, users can easily and quickly convert Excel data to XML format. IronXL offers a free Community Edition with limitations for non-commercial use. Paid versions of IronXL are available through subscription or perpetual-based licensing models, starting at $749. These paid versions offer enhanced features, support, and complete functionality. For the most up-to-date information on licensing, please refer to the IronXL website. To learn more about Iron Software products, visit this page.
9 .NET API products for your office documents