C# .xml to .xlsx how?

前端 未结 4 1754
甜味超标
甜味超标 2020-12-19 06:46

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 di

相关标签:
4条回答
  • 2020-12-19 07:23

    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
            ds.ReadXml(@"E:\movie.xml");
    
            //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)
            {
                iColumn++;
                excel.Cells[1, iColumn] = c.ColumnName;
            }
    
            // Row Data
            int iRow = worksheet.UsedRange.Rows.Count - 1;
    
            foreach (DataRow dr in dt.Rows)
            {
                iRow++;
    
                // Row's Cell Data
                iColumn = 0;
                foreach (DataColumn c in dt.Columns)
                {
                    iColumn++;
                    excel.Cells[iRow + 1, iColumn] = dr[c.ColumnName];
                }
            }
    
            ((Microsoft.Office.Interop.Excel._Worksheet)worksheet).Activate();
    
            //Save the workbook
            workbook.Save();
    
            //Close the Workbook
            workbook.Close();
    
            // Finally Quit the Application
            ((Microsoft.Office.Interop.Excel._Application)excel).Quit();
    
    0 讨论(0)
  • 2020-12-19 07:26

    You will need to read the schema for the XLSX file format and write an XSLT file to transform your custom XML file.

    .NET has very good XML support so something like this should be fairly trivial, it will be actual mapping from your XML format to XSLT where the real effort is needed.

    0 讨论(0)
  • 2020-12-19 07:39

    Please Use the below code if you are getting multiple tables from dataset and you should take care of the logic of inserting data tables into excel.

        static void Main(string[] args)
        {
            DataSet ds = new DataSet();
    
            //Convert the XML into Dataset
            ds.ReadXml(@"E:\movies.xml");
    
            //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);
    
            foreach (DataTable tab in ds.Tables)
            {
                FromDataTableToExcel(tab,excel,workbook);
            }
    
            //Save the workbook
            workbook.Save();
    
            //Close the Workbook
            workbook.Close();
    
            // Finally Quit the Application
            ((Microsoft.Office.Interop.Excel._Application)excel).Quit();
    
        }
    
        static void FromDataTableToExcel(DataTable dt, Microsoft.Office.Interop.Excel.Application excel, Microsoft.Office.Interop.Excel.Workbook workbook)
        { 
            //Create worksheet object
            Microsoft.Office.Interop.Excel.Worksheet worksheet = workbook.ActiveSheet;
    
            // Column Headings
            int iColumn = worksheet.UsedRange.Columns.Count-1;
            int iColumn1 = iColumn;
            int iColumn2 = iColumn;
    
            foreach (DataColumn c in dt.Columns)
            {
                iColumn++;
                excel.Cells[1, iColumn] = c.ColumnName;
            }
    
            // Row Data
            int iRow = 0;
    
            foreach (DataRow dr in dt.Rows)
            {
                iRow++;
    
                // Row's Cell Data                
                foreach (DataColumn c in dt.Columns)
                {
                    iColumn1++;
                    excel.Cells[iRow + 1, iColumn1] = dr[c.ColumnName];
                }
    
                iColumn1 = iColumn2;
            }
    
            ((Microsoft.Office.Interop.Excel._Worksheet)worksheet).Activate();
    
        }
    
    0 讨论(0)
  • 2020-12-19 07:45

    A far more simpler solution: use data sources in Excel.

    1. Create a "template" Xslx that matches your need.
    2. For each xml data files, add a data connection to the xml file.
    3. Set up, if you want, the data connection to refresh each time the file is opened.

    This works out-of-the box, with no code at all (excluding of course the xml generation).

    Optionally, you may publish your Xml through an ASP.Net application (dynamically or not), and set up the data connection to gather the data from this asp.net app.

    0 讨论(0)
提交回复
热议问题