1.调用方法:

ExportExcel("application/ms-excel", "EXCEL名称.xls", GridView1, this.Page);


2.定义方法:

 /// <summary>
        /// 另外一种存为EXCEL的方法
        /// </summary>
        /// <param name="FileType"></param>
        /// <param name="FileName"></param>
        /// <param name="gs"></param>
        /// <param name="page"></param>
        /// Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
        public static void ExportExcel(string FileType, string FileName, GridView gs, System.Web.UI.Page page)
        {
            if (gs.Rows.Count >= 65535)
            {
                MessageShow("数据条数超过65535!无法导出请缩小查询范围!");
                return;
            }
            Microsoft.Office.Interop.Excel.ApplicationClass eapp = new Microsoft.Office.Interop.Excel.ApplicationClass();
            eapp.Application.Workbooks.Close();
            eapp.Quit();
            page.Response.Charset = "GB2312";
            //  page.Response.ContentEncoding = System.Text.Encoding.UTF7;
            page.Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
            page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            page.Response.ContentType = FileType;
            page.EnableViewState = false;
            StringWriter tw = new StringWriter();
            HtmlTextWriter hw = new HtmlTextWriter(tw);
            gs.RenderControl(hw);
            page.Response.Write(tw.ToString());
            page.Response.End();
        }

 

 

posted on 2013-07-08 17:19  努力实现目标  阅读(1317)  评论(0编辑  收藏  举报