一下代码把一个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();
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();
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货