在 asp.net 中读取上传的 .xls excel 文件时出错
我编写了一个程序,从数据库中读取一组记录,然后将其保存在数据网格中。从这里我让用户将文件下载为 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");
“请帮助”。谢谢。
I have made a program where I read set of records from the database and then save it in a datagrid. From here I make the user download the file as a excel file.
Now the user make changes to the file and then upload it again and from here I read the uploaded .xls file and make the necessary changes in the DB.
The problem is whenever the user is uploading the updated excel file, I cannot access it and get an error.
External table is not in the expected
format
I make the user download the file as
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();
}
}
The user upload the file after making changes and I am reading it as
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");
Kindly help. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您正在生成的 XLS 文件的版本是否可能无法被 OLEDB 读取? Excel 8 是 Excel 97,您可能会生成 Excel 2007 .xlsx 格式。
Is it possible that the version of XLS file that you are generating cannot be read by OLEDB? Excel 8 is Excel 97 and you will likely be generating an Excel 2007 .xlsx format.
是的,我同意布莱恩!您可能会生成您想要读取的不同 .xlsx 格式。
试试这个: http:// www.aspdotnet-suresh.com/2010/09/import-data-from-excel-to-sql-database.html
Yes i agree to brian! you might be generating a different .xlsx format that you wants to read.
Try this: http://www.aspdotnet-suresh.com/2010/09/import-data-from-excel-to-sql-database.html