html導出到excel數據格式不正確解決方法


  同事離職 ,把手頭的程序給我了,第二天客戶說:“為什么我們導出的excel不會自己匯總啊??”

。。。。您稍等一下,我看一下哈(我怎么知道你們導出的excel為什么不會匯總啊!!)

打開程序

沒什么問題,然后導出打開

這是神馬?合着你導出來的全部是文本格式的了。文本格式存儲的數字怎么匯總你告訴我。

看代碼吧

 1 void exportexcel()
 2     {
 3         DataTable dt = getDataTable();
 4         if (dt != null)
 5         {
 6             Response.Clear();
 7             Response.Buffer = true;
 8             Response.Charset = "GB2312";
 9             Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(exceltitle.Value, System.Text.Encoding.UTF8) + ".xls");
10             Response.ContentEncoding = System.Text.Encoding.UTF8; 
11 
12             Response.ContentType = "application/ms-excel";
13             this.EnableViewState = false;
14             System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
15             System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
16 
17             gvExport = new System.Web.UI.WebControls.GridView();
18             gvExport.DataSource = dt.DefaultView;
19             gvExport.AllowPaging = false;
20             gvExport.DataBind();
21             gvExport.Attributes.Add("style", "vnd.ms-excel.numberformat:@");
22             // 返回客戶端 
23             gvExport.RenderControl(oHtmlTextWriter);
24             Response.Write(oStringWriter.ToString());
25             Response.End();
26 
27  
28         }
29         close();
30     }
View Code

excel的數據格式:gvExport.Attributes.Add("style", "vnd.ms-excel.numberformat:@");

下面是常用的一些格式:
1)文本:vnd.ms-excel.numberformat:@  
2)日期:vnd.ms-excel.numberformat:yyyy/mm/dd 
3)數字:vnd.ms-excel.numberformat:#,##0.00 
4)貨幣:vnd.ms-excel.numberformat:¥#,##0.00 
5)百分比:vnd.ms-excel.numberformat: #0.00% 

你這里輸出的格式就是文本啊!果斷的改成第三條數字類型的(哈哈,我是不是很聰明)

等等,我表頭的日期怎么回事,也變成數字了,看到它還有兩位小數的就撓頭了(失誤,失誤哈)

gvExport.HeaderRow.Attributes.Add("style", "vnd.ms-excel.numberformat:@");    

gvExport.Attributes.Add("style", "vnd.ms-excel.numberformat:#,##0.00");
把表頭改成原來的就行了,咱也不去改成時間類型了。
ok!完美,perfect!!!
 
 
本文由@巴黎_帥哥 原創,未經同意不得轉載


免責聲明!

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



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