我制作了一个程序,从数据库中读取一组记录,然后将其保存在数据网格中。从这里我让用户下载文件作为excel文件。在asp.net中读取上传的.xls excel文件时出错
现在用户对文件进行更改,然后再次上传,然后从此处读取上传的.xls文件并在数据库中进行必要的更改。
问题是每当用户上传更新的excel文件时,我无法访问它并出现错误。
外部表不在预期 格式
我让用户下载的文件作为
System.Web.HttpResponse response = System.Web.HttpContext.Current.Response;
// first let's clean up the response.object
response.Clear();
response.Charset = "";
// set the response mime type for excel
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", "attachment;filename=\"" + filename + "\"");
// create a string writer
using (System.IO.StringWriter sw = new System.IO.StringWriter())
{
using (System.Web.UI.HtmlTextWriter htw = new System.Web.UI.HtmlTextWriter(sw))
{
// instantiate a datagrid
System.Web.UI.WebControls.DataGrid dg = new System.Web.UI.WebControls.DataGrid();
dg.DataSource = dt;
dg.DataBind();
dg.RenderControl(htw);
response.Write(sw.ToString());
response.End();
}
}
用户上传的文件进行更改后,我读它
string connString = "Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + path + "\\" + FileUpload1.FileName + ";Extended Properties=Excel 8.0;";
OleDbConnection oledbConn = new OleDbConnection(connString);
oledbConn.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * FROM [$Sheet1 ", oledbConn);
OleDbDataAdapter oleda = new OleDbDataAdapter();
oleda.SelectCommand = cmd;
DataSet ds = new DataSet();
oleda.Fill(ds, "Employees");
请帮忙。谢谢。