xls-xml文件为CSV

时间:2012-10-24 08:29:11

标签: c# xml excel

我有一个黑盒子系统收到的文件,该文件以某种方式混合在xml和excel之间,当我用excel打开文件时,我首先得到一条警告信息,如果我用文本编辑器打开它,下面的xml头是:

<?xml version="1.0" encoding="UTF-8"?>
    <?mso-application progid="Excel.Sheet"?>
    <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:o="urn:schemas-microsoft-com:office:office"
        xmlns:x="urn:schemas-microsoft-com:office:excel"
        xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
        xmlns:html="http://www.w3.org/TR/REC-html40">

        <Styles>

                <Style ss:ID="Default">
                    <Alignment ss:Horizontal="Left" ss:Vertical="Bottom" />
                    <Borders/>
                    <Font/>
                    <Interior/>
                    <NumberFormat/>
                    <Protection/>
                </Style>
                <Style ss:ID="sHeader">
                    <Alignment ss:Horizontal="Left" ss:Vertical="Bottom" />
                    <Font ss:Bold="1"/>
                    <NumberFormat ss:Format="@"/>
                </Style>

我尝试了很多解决方案包括(首先尝试将文件导入DataGridView然后将其导出到csv但是我总是得到无法识别的数据库格式

首先,这是什么样的xls与xml文件混合?如何删除所有这些头信息,只需要一个简单的csv文件?

更新: 我找到了一种从这个excel-XML文件加载数据的方法,但我收到了一列中的所有数据

这是我使用的代码:

 XmlDocument xml = new XmlDocument();
            string filePath = @"C:\temp\test.xml";
            xml.Load(filePath);
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xml.NameTable);
            nsmgr.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
            XmlElement root = xml.DocumentElement;
            XmlNodeList nodeList = root.SelectNodes("//ss:Data", nsmgr);
            dataGridView1.DataSource= ConvertXmlNodeListToDataTable(nodeList);


public static DataTable ConvertXmlNodeListToDataTable(XmlNodeList xnl)
        {

            DataTable dt = new DataTable();

            int TempColumn = 0;



            foreach (XmlNode node in xnl.Item(0).ChildNodes)
            {

                TempColumn++;

                DataColumn dc = new DataColumn(node.Name, System.Type.GetType("System.String"));

                if (dt.Columns.Contains(node.Name))
                {

                    dt.Columns.Add(dc.ColumnName = dc.ColumnName + TempColumn.ToString());

                }

                else
                {

                    dt.Columns.Add(dc);

                }

            }

            int ColumnsCount = dt.Columns.Count;
            for (int i = 0; i < xnl.Count; i++)
            {

                DataRow dr = dt.NewRow();

                for (int j = 0; j < ColumnsCount; j++)
                {

                    dr[j] = xnl.Item(i).ChildNodes[j].InnerText;

                }

                dt.Rows.Add(dr);

            }

            return dt;

        }

    }

1 个答案:

答案 0 :(得分:0)

我找到了解决方案:

使用microsoft名称空间加载xls-xml文件 获取xmlNodeList

请注意,在我所在的地区,我们使用分号作为分隔符

public static XmlNodeList ParseExcelEXMLFormat(string filePath)
   {
       try
       {

            XmlDocument xml = new XmlDocument();
            xml.Load(filePath);
            XmlNamespaceManager nsSchema = new XmlNamespaceManager(xml.NameTable);
            nsSchema.AddNamespace("ss", "urn:schemas-microsoft-com:office:spreadsheet");
            XmlElement root = xml.DocumentElement;
            XmlNodeList nodeList = root.SelectNodes("//ss:Data", nsSchema);
            return nodeList;
       }
       catch (Exception)
       {

           throw;
       }
   }

然后将XmlNodeList转换为StringBuilder

 public static StringBuilder XMLNodeListToStringBuilderConverter(XmlNodeList xmlNodeList, string separator)
   {
       try
       {
           StringBuilder sb = new StringBuilder();
           DataTable dt = new DataTable();
           foreach (XmlNode node in xmlNodeList.Item(0).ChildNodes) 
           {
               DataColumn dc = new DataColumn(node.FirstChild.InnerText, System.Type.GetType("System.String"));
               dt.Columns.Add(dc);
           }

           int ColumnsCount = dt.Columns.Count;

           string[] columnNames = dt.Columns.Cast<DataColumn>().
                                             Select(column => column.ColumnName).
                                             ToArray();
           sb.AppendLine(string.Join(separator, columnNames));

           string[] rows = new string[ColumnsCount];

           for (int i = 1; i < xmlNodeList.Count; i++) // loop through rows
           {
               for (int j = 0; j < ColumnsCount; j++) // loop through columns
               {

                   rows[j] = xmlNodeList.Item(i).ChildNodes[j].InnerText.Replace(separator, ",").Replace("\r\n", " ").Replace("\n", " ").Replace("\r", " "); // remove seperator from original text, it will casue problem 

               }
               sb.AppendLine(string.Join(separator, rows));
               Array.Clear(rows, 0, ColumnsCount);
           }

           return sb;
       }
       catch (Exception)
       {

           throw;
       }
   }