【问题标题】:How to Get Value From Excel By Sheet ?如何通过工作表从 Excel 中获取价值?
【发布时间】:2015-06-19 21:46:43
【问题描述】:

下面是我的代码,这里一切正常,但产生问题的一件事是我从 excel 表中插入数据库的属性没有得到确切的值。如果它们包含,循环也会从其他表中获取值相同的属性名称。

这是我的代码

 string sqlConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + outputFile + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                    //Create Connection to Excel work book and add oledb namespace
                    OleDbConnection excelConnection = new OleDbConnection(sqlConnectionString);
                    excelConnection.Open();
                    DataTable dt = new DataTable();

                    dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                    if (dt == null)
                    {
                        return null;
                    }

                    String[] excelSheets = new String[dt.Rows.Count];
                    int t = 0;
                    //excel data saves in temp file here.
                    foreach (DataRow row in dt.Rows)
                    {
                        excelSheets[t] = row["TABLE_NAME"].ToString();
                        t++;
                    }
                    OleDbConnection excelConnection1 = new OleDbConnection(sqlConnectionString);
                    DataSet ds = new DataSet();
                    for (int i = 0; i <= excelSheets.Length - 1; i++)
                    {
                        string query = string.Format("Select * from [{0}]", excelSheets[i]);
                        using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                        {
                            dataAdapter.Fill(ds);
                        }

                       //Debug.Write("UserinfoId==" + ds.Tables[0].Rows[0]["UserInfoID"].ToString());
                        for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                        {
                            SqlConnection sqlc = new SqlConnection();
                            sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                            if (excelSheets[i] == "IdentityUser$")
                            {
                                //Debug.Write("Id==" + ds.Tables[0].Rows[j]["Id"].ToString() + "\n");

                            }
                            else if (excelSheets[i] == "USERINFO$")
                            {                              

                                //Debug.Write("UserinfoId==" + ds.Tables[0].Rows[j]["UserInfoID"].ToString()+"\n");
                            }

现在,当我从 Excel 表 USERINFO$ 插入 userinfoId 时,它也在从 IdentityUser$ 表中搜索,因为我也从该表中获取了 UserinfoId

【问题讨论】:

  • 尝试将循环内的 DataSet ds = new DataSet() 移动到工作表上。我认为,如果您在数据表上调用 Fill 2 次,这些值将被合并,因此您将从表 1 中获取值与表 2 中的值。这就是为什么在表 2 上没有值的地方,您会得到表 1 中的值
  • @AndreiNeagu 非常感谢它解决了我的问题

标签: sql-server asp.net-mvc-4 c#-4.0 asp.net-mvc-5


【解决方案1】:

这解决了我的问题。我已将DataSet ds = new DataSet(); 放入 for 循环中,它可以工作。

    string sqlConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + outputFile + ";Extended Properties=Excel 12.0;Persist Security Info=False";
                        //Create Connection to Excel work book and add oledb namespace
                        OleDbConnection excelConnection = new OleDbConnection(sqlConnectionString);
                        excelConnection.Open();
                        DataTable dt = new DataTable();

                        dt = excelConnection.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
                        if (dt == null)
                        {
                            return null;
                        }

                        String[] excelSheets = new String[dt.Rows.Count];
                        int t = 0;
                        //excel data saves in temp file here.
                        foreach (DataRow row in dt.Rows)
                        {
                            excelSheets[t] = row["TABLE_NAME"].ToString();
                            t++;
                        }
                        OleDbConnection excelConnection1 = new OleDbConnection(sqlConnectionString);                   
                        for (int i = 0; i <= excelSheets.Length - 1; i++)
                        {
                            DataSet ds = new DataSet();
                            string query = string.Format("Select * from [{0}]", excelSheets[i]);
                            using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter(query, excelConnection1))
                            {
                                dataAdapter.Fill(ds);
                            }


                                    for (int j = 0; j < ds.Tables[0].Rows.Count; j++)
                                    {
                                        SqlConnection sqlc = new SqlConnection();
                                        sqlc.ConnectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
                                        if (excelSheets[i] == "IdentityUser$")
                                        {                                   

                                        }
                                        else if (excelSheets[i] == "USERINFO$")
                                        { 
                                        }
                                      }
                                  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-24
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2011-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多