在将数据从Excel文件转换为XML时,Dot被隐式转换为哈希

use*_*552 5 c# xml oledb excel

我已成功使用以下C#代码从Excel文件创建XML文件:

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]";
    }
}
Run Code Online (Sandbox Code Playgroud)

但问题是当此代码将Excel数据转换为XML数据时,则本身会转换为Hash(仅第一行).我知道原因,但不知道解决方案.
它是因为Excel文件中的转换成XML标签时发生的,它们隐式转换为HASH .......
请建议我,如何阻止这种转换?

use*_*552 4

终于得到了解决方案:

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

  2. 现在我已将该数据存储到 DataTable(dt) 中,然后访问列名称并将 HASH 替换为 DOT(使用 String 的 Replace 方法),并使用新列名称创建一个新的 DataTable(dt2)。

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

  4. 最后用 new DataTable(dt2) 绑定网格

以下是该函数的完整代码:

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]";
}
Run Code Online (Sandbox Code Playgroud)

以下是代码write_to_xml()

public void write_to_xml(DataTable dt, string path)
{
    dt.WriteXml(path);
}
Run Code Online (Sandbox Code Playgroud)

任何查询或替代解决方案将不胜感激......:)