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