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

Response输出excel设置文本样式

时间:2016-03-28 16:43:50      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:

在网上查了些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样式的效果

Response输出excel设置文本样式

标签:

原文地址:http://www.cnblogs.com/midcn/p/5329556.html

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