How to Convert XLS file to XML using C#

Asked By 0 points N/A Posted on -

Hi all,

I want to convert an XLS file to XML file automatically.

i am doing this by using manual method like in this code.

Microsoft.Office.Interop.Excel.Sheets sheets = theWorkbook.Worksheets;
    Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)sheets.get_Item(1);
       range = worksheet.UsedRange;

                for (rcnt = 1; rcnt <= range.Rows.Count; rcnt++)
                    using (XmlWriter writer = XmlWriter.Create("Demo.xml"))
                        writer.WriteElementString("Name", (string)(range.Cells[rcnt, 1] as Microsoft.Office.Interop.Excel.Range).Value2);
                        writer.WriteElementString("Fname", (string)(range.Cells[rcnt, 2] as Microsoft.Office.Interop.Excel.Range).Value2);
                        writer.WriteElementString("Country", (string)(range.Cells[rcnt, 3] as Microsoft.Office.Interop.Excel.Range).Value2);


Is there is anyother function which can be used to easily convert file?

Answered By 0 points N/A #117531

How to Convert XLS file to XML using C#


If you've got ready a workbook, you simply need to use the workbook.SaveAsXml() methodology. In this methodology, you will offer the name of the file in xml format. In this demo, I tend to produce a workbook, and set the background color of some ranges, and then we tend to convert it to xml file.
The following code shows us the strategy to convert Excel to XML with C#.

C# code: Assuming a file named spire.xls

using Spire.Xls; //Spire.xls represents the file name
using System.Drawing;
 namespace ConvertToXml
    class Program
        static void Main(string[] args)
            //Create a workbook
            Workbook workbook = new Workbook();
            //Initialize the worksheet
            Worksheet sheet = workbook.Worksheets[0];
            //Set background color 
            sheet.Range["C10"].Style.KnownColor = ExcelColors.Gray25Percent;
            sheet.Range["C11"].Style.KnownColor = ExcelColors.Blue;
            //Save it as xml file
            //Launch the file



Login/Register to Answer

Related Questions