Fridolin Fridolin -3 years ago 189
C# Question

C# .xml to .xlsx how?

I want to convert a complete XML file to XLSX but I'm not sure how I can do it.
I searched at Google for a Solutions but the most time I only find the way into the other direction for example XLSX to XML.
On the Microsoft Page I found a xmlconvertclass, but I'm not sure how I can work with the class.

Did someone do something like this in the past and can help me?

Answer Source

Try the Below code in which i converted XML into DataSet and later exported DataSet into Excel

        DataSet ds = new DataSet();

        //Convert the XML into Dataset

        //Retrieve the table fron Dataset
        DataTable dt = ds.Tables[0];

        // Create an Excel object
        Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();

        //Create workbook object
        string str = @"E:\test.xlsx";
        Microsoft.Office.Interop.Excel.Workbook workbook = excel.Workbooks.Open(Filename: str);

        //Create worksheet object
        Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;

        // Column Headings
        int iColumn = 0;

        foreach (DataColumn c in dt.Columns)
            excel.Cells[1, iColumn] = c.ColumnName;

        // Row Data
        int iRow = worksheet.UsedRange.Rows.Count - 1;

        foreach (DataRow dr in dt.Rows)

            // Row's Cell Data
            iColumn = 0;
            foreach (DataColumn c in dt.Columns)
                excel.Cells[iRow + 1, iColumn] = dr[c.ColumnName];


        //Save the workbook

        //Close the Workbook

        // Finally Quit the Application
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download