【问题标题】:Import Excel Sheet in Asp.Net在 Asp.Net 中导入 Excel 工作表
【发布时间】:2016-09-09 01:28:50
【问题描述】:

我需要问一下关于导入excel表格的问题。

下面我有一个示例表

那么我需要像这种格式那样导入这个表-->

        IndustryName | IndustryCode | CategoryName | CategoryCode | CategoryValue | SubCategoryName | SubCategoryCode | SubCategoryValue
        IndustryName1  IndustCode     CatName        CatCode        CatValue        SubCatName        SubCatCode        SubCatValue
        IndustryName1  IndustCode     CatName        CatCode        CatValue        SubCatName        SubCatCode        SubCatValue

在 SubCategory 行 SubCategoryCode 前面有两个空格。它是固定的。

有人知道我该怎么做吗?

【问题讨论】:

    标签: c# asp.net sql-server excel import


    【解决方案1】:

    您可以将您的 excel 数据导入数据集,然后使用数据集并创建您想要的表。例如,当遍历第一列时,您可以检查字符串是否有 2 个空格,然后将其添加到子类别列,其他列添加到类别列。您所要做的就是遍历 DataSet 的行并将所需的值存储在新的数据表中。

    public DataSet ExcelToDS(string Path, string sheetname)
            {
                string xlsConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path + ";Extended Properties=Excel 8.0;";
                string xlsxConn = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Path + ";Extended Properties=Excel 12.0";
                using (OleDbConnection conn = new OleDbConnection(xlsxConn))
                {
                    conn.Open();
                    string strExcel = "select * from [" + sheetname + "$]";
                    OleDbDataAdapter oledbda = new OleDbDataAdapter(strExcel, xlsxConn);
                    DataSet ds = new DataSet();
                    oledbda.Fill(ds, sheetname);
                    conn.Close();
                    return ds;
                }
            }
    

    【讨论】:

    • 感谢您的回复。但我的问题是不导入 excel 表。实际如何转换成以上sql格式。
    • 我已经写过,您需要遍历可用的行并通过在其中存储所需的值来生成所需的表。@ekremtapan
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-05
    • 2015-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多