Introduction
Excel is a Microsoft Office component. We often need to convert an Excel document to a tag-based format like XML.
Here I will write a program in C# to convert an Excel file to XML.
First of all we see our requirement of what type of XML file we want, in other words which column comes first and so on.
So, here we are starting.
Excel file
Here we have an Excel file named Birthday list Feb 15 for team.xlsx.
In which we have 5 columns and we sorted them by day in ascending order.
And now we will convert this file to a XML document.
So now start your Visual Studio.
Create a new project, ASP.NET web application, named ExcelXml.
In Solution Explorer click on Default.aspx.
And place the following 3 controls:
- A file upload control.
- A button.
- A Gridvew to display Excel data is coming or not.
Now we will write C# code to convert an Excel file. So in Default.aspx.cs we write our code as in the following:
Now we need to build our program.
Press F6 to build.
Run the project.
Press F5.
Choose your file from your desktop or where you put your file.
Then click the button.
You get the following result in your browser.
To find the desired output file in XML go to your desktop and look for Output.xml.
Here it is.
Output.xml
Conclusion
So in this way we convert an Excel file to XML. One thing we need to remember is that we need to sort the columns of the Excel file depending on which column comes first.