我使用如下代码把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就有此问题。请问如何解决?

解决方案 »

  1.   

    http://hi.baidu.com/bluelight68/blog/item/032efbd27d9e8cd8a8ec9ad6.html
      

  2.   

    "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\file1.xls;Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";
      

  3.   

    在到处Excel时,你可以先在目录下建立一个空的Excel文件,然后把它当做数据库,在代码里连接它,再把其他数据库中的数据写进来。
    这样文件肯定没有问题
      

  4.   

    直接用这个就可以了。"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = D:\file1.xls;Extended Properties ='Excel 8.0;HDR=NO;IMEX=1'";