数据库引擎找不到对象'Sheet1$'。请确定对象是否存在,并正确地写出它的名称和路径
请帮我看看为什么总是报这个错误。protectedvoidbtnReadDataTo_Click(objectsender,EventArgse){if(FuloadEx...
请帮我看看为什么总是报这个错误。
protected void btnReadDataTo_Click(object sender, EventArgs e)
{
if (FuloadExcelFile.FileName == "")
return;
string fileExt = System.IO.Path.GetExtension(FuloadExcelFile.FileName);
if (fileExt != ".xls")//必须是EXCEL文件
return;
string filepath = FuloadExcelFile.PostedFile.FileName;//文件路径
DataTable dt = new DataTable();
dt = CallExcel(filepath);//返回EXCEL文件的数据
// if (InsertSQLServer(dt, FuloadExcelFile.FileName.Split('.')[0]))//导入数据库
if (InsertSQLServer(dt, "WagMan"))//导入数据库
{
Response.Redirect("EmployeeMain.aspx");
}
}
protected DataTable CallExcel(string filepath)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0");
con.Open();
string sql = "select * from [Sheet1$]";//选择第一个数据SHEET
//OleDbCommand command = new OleDbCommand(sql, con);
//OleDbDataReader reader = command.ExecuteReader();
//if (reader.Read())
//{
// reader[0].ToString();//直接读出数据
//}
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
//reader.Close();
//command.Dispose();
con.Close();
con.Dispose();
return dt;
} 展开
protected void btnReadDataTo_Click(object sender, EventArgs e)
{
if (FuloadExcelFile.FileName == "")
return;
string fileExt = System.IO.Path.GetExtension(FuloadExcelFile.FileName);
if (fileExt != ".xls")//必须是EXCEL文件
return;
string filepath = FuloadExcelFile.PostedFile.FileName;//文件路径
DataTable dt = new DataTable();
dt = CallExcel(filepath);//返回EXCEL文件的数据
// if (InsertSQLServer(dt, FuloadExcelFile.FileName.Split('.')[0]))//导入数据库
if (InsertSQLServer(dt, "WagMan"))//导入数据库
{
Response.Redirect("EmployeeMain.aspx");
}
}
protected DataTable CallExcel(string filepath)
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filepath + ";Extended Properties=Excel 8.0");
con.Open();
string sql = "select * from [Sheet1$]";//选择第一个数据SHEET
//OleDbCommand command = new OleDbCommand(sql, con);
//OleDbDataReader reader = command.ExecuteReader();
//if (reader.Read())
//{
// reader[0].ToString();//直接读出数据
//}
OleDbDataAdapter adapter = new OleDbDataAdapter(sql, con);
DataTable dt = new DataTable();
adapter.Fill(dt);
//reader.Close();
//command.Dispose();
con.Close();
con.Dispose();
return dt;
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询