码迷,mamicode.com
首页 > 其他好文 > 详细

有gridview汇出word和excel

时间:2015-07-24 15:42:01      阅读:106      评论:0      收藏:0      [点我收藏+]

标签:

    private void Export(GridView _gv, string filetype, string FileName)
    {
        if (filetype == "1")
        {
            string style = @"<style> .text { mso-number-format:\@; } </script> ";
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());//FileName.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/excel";//设置输出文件类型为excel文件。
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            _gv.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
        else
        {
            string style = @"<style> .text { mso-number-format:\@; } </script> ";
            Response.Clear();
            Response.Buffer = true;
            Response.Charset = "GB2312";
            Response.Write("<meta http-equiv=Content-Type content=text/html;charset=GB2312>");
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, Encoding.UTF8).ToString());
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            Response.ContentType = "application/word";//设置输出文件类型为Word文件。
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
            _gv.RenderControl(oHtmlTextWriter);
            Response.Output.Write(oStringWriter.ToString());
            Response.Flush();
            Response.End();
        }
    }

有gridview汇出word和excel

标签:

原文地址:http://www.cnblogs.com/sdya/p/4673579.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!