有gridview汇出word和excel

    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();
        }
    }

posted @ 2015-07-24 15:14  好好學習  阅读(187)  评论(0编辑  收藏  举报