c# - 在将数据从 Excel 文件转换为 XML 时,点被隐式转换为散列

标签 c# xml excel oledb

我已成功使用以下 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]";
    }
}

但问题是当此代码将 Excel 数据转换为 XML 数据时, 本身会转换为哈希(仅第一行)。知道原因,不知道解决办法。
发生这种情况是因为 Excel 文件中的 在转换为 XML 标记时会隐式转换为 HASH......
请建议我,如何停止此转换?

最佳答案

终于得到解决方案:

  1. OLEDB Adapter 在DataSet 中填充数据时,将DOT 转换为HASH。

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

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

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

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

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);
}

如有任何疑问或替代解决方案,我们将不胜感激... :)

关于c# - 在将数据从 Excel 文件转换为 XML 时,点被隐式转换为散列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9260172/

相关文章:

c++ - 我如何在 TinyXML 中获取标签的名称?

arrays - 从数组中合并 Excel 中的单元格

c# - 单击链接按钮时清除复选框列表选择(尽管回发检查)

c# - MVVM 共享代码 Win8 和 WP8 以及文件访问

c# - MSBUILD 引发错误 : The SDK 'Microsoft.NET.Sdk' specified could not be found

vba - 在VBA中将big-endian转换为little-endian,反之亦然

excel - VBA:如果在引用表中找到值,则将数据+字段从源表拉到目标表

c# - 从蓝牙接收字符串后使用线程更新标签

sql - 如何更新postgresql中的xml数据类型列

xml - 如何阻止 Eclipse 尝试运行 XML 文件?