我使用如下代码把gridview导出到一个excel文件中
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=file1.xls"); StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End(); 在另外一个页面,我使用如下代码读取该Excel的内容:
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source = D:\file1.xls")
myConn.Open();
此时,有异常:"外部表不是预期的格式。" 我用OledbConnection读取手工创建的excel都没有问题,但读取我用程序导出的Excel就有此问题。请问如何解决?
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "GB2312";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF7;
HttpContext.Current.Response.ContentType = "application/excel";
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=file1.xls"); StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
GridView1.RenderControl(htw); HttpContext.Current.Response.Write(sw.ToString());
HttpContext.Current.Response.End(); 在另外一个页面,我使用如下代码读取该Excel的内容:
OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Extended Properties=Excel 8.0;Data Source = D:\file1.xls")
myConn.Open();
此时,有异常:"外部表不是预期的格式。" 我用OledbConnection读取手工创建的excel都没有问题,但读取我用程序导出的Excel就有此问题。请问如何解决?
这样文件肯定没有问题