一下代码把一个Excel中Sheet1数据的导入到数据库:在vs2008下是可以的,但是我一发布就出错了,总是提示“Microsoft Jet 数据库引擎找不到对象'Sheet1$'。请确定对象是否存在,并正确地写出它的名称和路径。”,请问这是怎么回事啊???
            string pwd = FormsAuthentication.HashPasswordForStoringInConfigFile("123456", "MD5");
            string filename = this.img.PostedFile.FileName;
            int index = filename.LastIndexOf("\\");
            string file = filename.Substring(index + 1, filename.Length - index - 5);
            string conn = " Provider = Microsoft.Jet.OLEDB.4.0 ; Data Source =" + filename + ";Extended Properties='Excel 8.0;HDR=NO;'";
            OleDbConnection thisconnection = new OleDbConnection(conn);
            thisconnection.Open();
            string Sql = "select * from [Sheet1$]";
            OleDbDataAdapter mycommand = new OleDbDataAdapter(Sql, thisconnection);
            System.Data.DataSet ds = new System.Data.DataSet();
            mycommand.Fill(ds, "ds");
            thisconnection.Close();            string conn1 = "User ID=sa;Data Source=127.0.0.1;Password=;Initial Catalog=Lecture;Provider=SQLOLEDB.1;";
            OleDbConnection thisconnection1 = new OleDbConnection(conn1);
            thisconnection1.Open();
            int count = ds.Tables["ds"].Rows.Count;
            for (int i = 0; i < count; i++)
            { 
                string id, id_1, id_2, id_3;
                id = ds.Tables["ds"].Rows[i][0].ToString();
                id_1 = ds.Tables["ds"].Rows[i][1].ToString();
                id_2 = ds.Tables["ds"].Rows[i][2].ToString();
                id_3 = ds.Tables["ds"].Rows[i][3].ToString();
                string excelsql = "insert into Stu(SNo, SName, SSex, SClass, SPwd) values ('" + id + "','" + id_1 + "','" + id_2 + "','" + id_3 + "', '"+pwd+"') ";
                OleDbCommand mycommand1 = new OleDbCommand(excelsql, thisconnection1);
                mycommand1.ExecuteNonQuery();
            }
            Response.Write("更新成功"); 
            thisconnection1.Close();  

解决方案 »

  1.   

     Data Source =" + filename + ";你的文件名正确么?还有你的那个表格名是否有改过
      

  2.   

    文件名 和  EXCEL里面的表格一定要对应,如果没指定的话。
      

  3.   

    filename 写正确就OK!debug一下!!
      

  4.   

    发布后路径已变了,如果是在客户端则excel还在客户端,要服务器当然找不到excel了,最好是先上传到服务器,然后再去读取上传后的excel中的数据,或在客户端用流读取数据提交.