0

我有一个从黑盒系统收到的文件,该文件以某种方式在 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;

        }

    }
4

1 回答 1

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;
       }
   }
于 2012-10-30T14:10:44.937 回答