Response输出excel设置文本样式

在网上查了些Response导出excel然后设置样式的方法,发现没有一个可行的于是开始自己研究,

发现可以通过输出样式的方式进行配置,我要设置的是全文本格式在excel样式是这样的mso-number-format:"\@" 

于是我对Response输出进行了完善

            Response.Clear();
            Response.BufferOutput = true;
            string style = "<style> td{ mso-number-format:\"\\@\" } </style> ";
            //设定输出的字符集 
            Response.Charset = "GB2312";
            //假定导出的文件名为FileName.xls 
            Response.AppendHeader("Content-Disposition", "attachment;filename= tiaoma.xls");
            Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
            //设置导出文件的格式 
            Response.ContentType = "application/ms-excel";

            EnableViewState = false;

            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
            System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);

            System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);

            GridView gv = new GridView();
            gv.DataSource = Session["table"] as DataTable;
            gv.DataBind();
            gv.RenderControl(textWriter);

            Response.Write(style);
            Response.Write(stringWriter.ToString());
            Response.End();

成功的实现了Response输出并设置excel样式的效果

posted @ 2016-03-28 16:35  .net万剑  阅读(3245)  评论(0编辑  收藏  举报