求救: excel上传到服务器ok,读取到gridview时出错,
异常详细信息: System.Data.OleDb.OleDbException: 无法启动应用程序。工作组信息文件丢失,或是已被其它用户以独占方式打开。 代码如下!protected void btUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert('请选择要上传的文件!');</script>");
return;
}
else
{
string path = this.Server.MapPath(".. \\Upfile\\") + FileUpload1.FileName;
FileUpload1.PostedFile.SaveAs(path);
FileUpload1.Dispose();
string strCon;
strCon = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Secruity Info=true;Data Source=" + path + ";user id=nw14134;Jet OLEDB:Database password=ccli2054;Extended Properties=\"Excel 8.0; HDR=Yes; IMEX=1;\"";
OleDbConnection conn = new OleDbConnection(strCon);
conn.Open();
OleDbDataAdapter myda = new OleDbDataAdapter("select * from [Sheet1$]", strCon);
DataSet myds = new DataSet();
myda.Fill(myds);
this.GridView2.DataSource = myds;
this.GridView2.DataBind();
conn.Close();
System.IO.File.Delete(path);
} --------------------编程问答-------------------- 有个列子:
private DataSet CreateDataSource()
{
string strCon;
str="Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+Server.MapPath("excel.Xls")+";
Extebded Properties =Excel 8.0;";
OleDbConnection olecon =new OleDbConnection(strCon);
OleDbDaraAdapter myda =new OleDbDaraAdapter ("select * from[sheet1$]",strCon);
DataSet myds =new DataSet();
myda.Fill(myds);
return myds;
}
//按钮事件
protected viod Button1_Click(object sender,EventArgs e)
{
GridView2.DataSource=CreateDataSource();
GridView2.DataBind();
}
一个一个敲的呀。。为了这分我不容易呀。。 --------------------编程问答-------------------- 是这样的,如果有其他的程序打开了那个文件,就不能读取了,把excel关掉再读就可以了。 --------------------编程问答--------------------
DataTable Excel_UserInfo = new DataTable();
string strConn = @"Provider=Microsoft.ACE.OLEDB.12.0;" + "Data Source=" + fileInfo.FullName + ";" + "Extended Properties=\"Excel 12.0;HDR=YES;IMEX=1;\"";
string strExcel = "select * from [sheet1$]";
using (OleDbDataAdapter adaptor = new OleDbDataAdapter(strExcel, strConn))
{
DataSet ds = new DataSet();
adaptor.Fill(ds);
Excel_UserInfo = ds.Tables[0];
}
剩下绑定的靠自己了。。 --------------------编程问答-------------------- 谢谢两位,我的代码有错吗,请指正! --------------------编程问答-------------------- 另外,我的代码,在本地OK 传到服务器上才出这个问题 --------------------编程问答-------------------- 服务器上有office组件吗? --------------------编程问答-------------------- 应该没有,是万网的服务器
补充:.NET技术 , ASP.NET