当前位置:编程学习 > C#/ASP.NET >>

导出数据到Excel的问题

下面是导出Excel的代码,但是现在的问题是不兼容office 2007,只能在2003下面打开,有没有哪位大侠遇到过这个问题麻烦指点下,谢谢 
Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("班级学员培训信息统计.xls", System.Text.Encoding.UTF8));
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.gvInfo.RenderControl(oHtmlTextWriter);
        Response.Output.Write(oStringWriter.ToString());
        Response.Flush();
        Response.End(); --------------------编程问答-------------------- 解决方案

PS:Excel导出跟2007 还是2003没有决定关系 --------------------编程问答-------------------- 这是开发中的问题,经验证这个代码导出的Excel表格,用2003打开是没有问题,用2007打开会提示打开的文件格式与文件扩展名指定的格式不一致,网上查了很多人遇到这种情况,但是没有找到解决办法 --------------------编程问答-------------------- public void ExortExcel() //从GridView中导出Excel的函数
    {
        if (this.Gv.Rows.Count != 0)
        {
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls");     //这里是用日期做名称
            HttpContext.Current.Response.Charset = "utf-8";
            HttpContext.Current.Response.ContentType = "application/excel";
            System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            this.Gv.AllowPaging = false;                 //GridView不启用分页
            Gv.RenderControl(htmlWrite);
            HttpContext.Current.Response.Write(stringWrite.ToString());
        }

}

private void ToExcel() //从GridView导出Excel函数2 (这两个方法可任选其一)
    {
        string style = @"<style> .text { mso-number-format:\@; } </script> ";
        string Ftile = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + System.DateTime.Now.Hour.ToString();
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "GB2312";
        Response.AppendHeader("Content-Disposition", "attachment;filename=COM_" + Ftile + ".xls");
      //  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文
        Response.ContentEncoding = System.Text.Encoding.UTF7;
        Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
 //       Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
        this.EnableViewState = false;
        System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
        System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
        System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
        this.Gvexel.RenderControl(oHtmlTextWriter);//GVDY 是Gridview的ID名称
        Response.Write(style);   
        Response.Write(oStringWriter.ToString());
        Response.End();
    }

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/hongjiaoli/archive/2009/10/23/4716844.aspx --------------------编程问答-------------------- 这个我也遇到过,不过没关系啊用2007可以打开啊,点是就可以打开了。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,