1

来自选项卡式 Excel 电子表格 C# .NET 的嵌套 XML

我正在尝试将 excel 电子表格中多张工作表中的数据格式化为嵌套 xml

我想结束的xml是

<root>
<Tab>
    <TabName>Tab 0</TabName>
    <Products>
        <ItemId>11111</ItemId>
        <Image>/images/100.jpg</Image>
        <Link>http://www.google.com</Link>
    </Products>
    <Products>
        <ItemId>22222</ItemId>
        <Image>/images/100.jpg</Image>
        <Link>http://www.google.com</Link>
    </Products>
</Tab>
<Tab>
    <TabName>Tab 1</TabName>
    <Products>
        <ItemId>11111</ItemId>
        <Image>/images/100.jpg</Image>
        <Link>http://www.google.com</Link>
    </Products>
    <Products>
        <ItemId>22222</ItemId>
        <Image>/images/100.jpg</Image>
        <Link>http://www.google.com</Link>
    </Products>
</Tab>
</root>

我正在使用的代码是

public static string Ajax_ReadExcel()
{
    OleDbConnection objConn = null;
    System.Data.DataTable dt = null;
    string fileName = HttpRuntime.AppDomainAppPath + "ExcelUpload\\ProductData.xlsx";
    DataSet objDataset1 = new DataSet("root");

    try
    {
        // Connection String. Change the excel file to the file you
        // will search.
        String connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
          "Data Source=" + fileName + ";Extended Properties=Excel 8.0;";
        connString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName);
        // Create connection object by using the preceding connection string.
        objConn = new OleDbConnection(connString);
        // Open connection with the database.
        objConn.Open();
        // Get the data table containg the schema guid.
        dt = objConn.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
        //dt = objConn.E
        if (dt == null)
        {
            return null;
        }

        String[] excelSheets = new String[dt.Rows.Count];
        int i = 0;

        // Add the sheet name to the string array.
        foreach (DataRow row in dt.Rows)
        {
            excelSheets[i] = row["TABLE_NAME"].ToString();
            i++;
        }

        // Loop through all of the sheets if you want too...
        for (int j = 0; j < excelSheets.Length; j++)
        {
            OleDbCommand objCmdSelect = new OleDbCommand("SELECT * FROM ["+excelSheets[j]+"]", objConn);
            OleDbDataAdapter objAdapter1 = new OleDbDataAdapter();

            objAdapter1.SelectCommand = objCmdSelect;


            objAdapter1.Fill(objDataset1, "Products");
            // Query each excel sheet.
            //Trace.Write(objDataset1.GetXml());
        }

        //ltrContent.Text += "<hr/>" + excelSheets;

        objConn.Close();
        //return excelSheets;
    }
    catch (Exception ex)
    {
        return null;
    }
    finally
    {
        // Clean up.
        if (objConn != null)
        {
            objConn.Close();
            objConn.Dispose();
        }
        if (dt != null)
        {
            dt.Dispose();
        }
    }
    return objDataset1.GetXml();

}

我可以将所有的 excel 数据加载到一个 xml 集中

<root>
<Products>...</Products>
</root>

,但我对 C# DataSet 不够熟悉,无法将输出转换为我需要的嵌套 xml。

4

0 回答 0