在将Excel文件中的数据转换为XML时,将点隐式转换为散列

我已经成功地使用以下C#代码从Excel文件创buildXML文件:

protected void Button5_Click(object sender, EventArgs e) { if (FileUpload1.HasFile) { OleDbConnection ole = new OleDbConnection(); string s = Server.MapPath("../admin/ProductOptions"); s = s + "\\" + FileUpload1.FileName; System.IO.File.Delete(s); FileUpload1.PostedFile.SaveAs(s); string path = s; ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;" + "\""; OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole); DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(command); adapter.Fill(ds); GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); GridView1.Visible = true; string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml"; Session["ss"] = ds; write_to_xml(ds,filepath); } else { Label2.Visible = true; Label2.Text="[Please Select a file]"; } } 

但问题是,当这个代码将Excel数据转换为XML数据,然后本身被转换成散列(只有第一行)。 我知道原因,但不知道解决scheme。
这是因为在Excel文件中的转换为XML标签时,他们隐式转换为哈希…….
请告诉我,我怎样才能阻止这种转换?

终于得到了解决办法:

  1. 当OLEDB适配器填充DataSet中的数据时,它将DOT转换为HASH。

  2. 现在我已经将这些数据存储到DataTable(dt)中,然后访问列名并用DOTreplaceHASH(使用String的Replace方法),并用新的列名创build一个新的DataTable(dt2)。

  3. 在这之后使用两个for循环,我已经从第一个DataTable(dt)插入数据到新的Datatable(dt2)。 (*一个循环的行和另一个列)

  4. 最后用新的DataTable(dt2)绑定网格,

以下是该function的完整代码:

 if (FileUpload1.HasFile) { OleDbConnection ole = new OleDbConnection(); string s = Server.MapPath("../admin/ProductOptions"); s = s + "\\" + FileUpload1.FileName; FileUpload1.PostedFile.SaveAs(s); string path = s; ole.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + path + ";" + "Extended Properties=" + "\"" + "Excel 12.0;HDR=YES;IMEX=2;READONLY=FALSE;" + "\" "; OleDbCommand command = new OleDbCommand("select * from[SHEET1$]", ole); DataSet ds = new DataSet(); OleDbDataAdapter adapter = new OleDbDataAdapter(command); adapter.Fill(ds); DataTable dt = (DataTable)ds.Tables[0]; DataTable dt2 = new DataTable("dt2"); Session["dt"] = null; for (int i = 0; i < dt.Columns.Count; i++) { string s2 = dt.Columns[i].ToString(); s2 = s2.Replace("#", "."); string ProductName = s2.ToString(); if (Session["dt"] == null) { DataColumn dCol1 = new DataColumn(ProductName, typeof(System.String)); dt2.Columns.Add(dCol1); } } for (int i = 0; i < dt.Rows.Count; i++) { dt2.Rows.Add(); for (int x = 0; x < dt.Columns.Count; x++) { dt2.Rows[i][x] = dt.Rows[i][x]; } } System.IO.File.Delete(s); GridView1.DataSource = dt2; GridView1.DataBind(); GridView1.Visible = true; string filepath = Server.MapPath("ProductOptions") + "\\" + DDLproduct.SelectedValue + ".xml"; // Session["ss"] = ds; write_to_xml(dt2,filepath); } else { Label2.Visible = true; Label2.Text="[Please Select a file]"; } 

以下是write_to_xml()的代码:

 public void write_to_xml(DataTable dt, string path) { dt.WriteXml(path); } 

任何查询或替代解决scheme将不胜感激… 🙂

在你的连接string中用HDR=Noclosures你的头文件,完成你的工作。

在喂食他们之前,用HDR=Yesreplace.#使用正则expression式或任何你想在第一行的工具。

而不是你的解决scheme,我用这种方式使用Encoding.UTF8

 using (var fs = new FileStream(xmlFile, FileMode.CreateNew)) { using (var xw = new XmlTextWriter(fs, Encoding.UTF8)) { ds.WriteXml(xw); } } 

并没有问题,这也转换< to &lt;>&gt;