如何从Excel中获取价值?

时间:2015-04-14 10:13:41

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

下面是我的代码,这里一切正常,但是创建问题的一件事是我从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 Sheet USERINFO $插入userinfoId时,它也正在从IdentityUser $表中搜索,因为我也从该工作表中获取了UserinfoId

1 个答案:

答案 0 :(得分:0)

这解决了我的问题。我已将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$")
                                        { 
                                        }
                                      }
                                  }