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樣式的效果


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM