[Excel格式] 設定Excel cell的格式

利用網頁轉出資料到 Excel 上常會遇到想要以文字格式顯示的數字,像是手機號碼、帳號之類的資料,被 Excel 自動轉換成數字格式,於是前面的 0 被省略掉了、帳號變成科學記號了...等等的麻煩,有個方便的方式不會去加入奇奇怪怪的東西到欄位裡面,讓 Excel 不會去判斷成數字,很簡單,只要在 td 裡面加入 style 設定就行了。 

mso-number-format:"0"    NO Decimals
mso-number-format:"0\.000"    3 Decimals
mso-number-format:"\#\,\#\#0\.000"    Comma with 3 dec
mso-number-format:"mm\/dd\/yy"    Date7
mso-number-format:"mmmm\ d\,\ yyyy"    Date9
mso-number-format:"m\/d\/yy\ h\:mm\ AM\/PM"    D -T AMPM
mso-number-format:"Short Date"    01/03/1998
mso-number-format:"Medium Date"    01-mar-98
mso-number-format:"d\-mmm\-yyyy"    01-mar-1998
mso-number-format:"Short Time"    5:16
mso-number-format:"Medium Time"    5:16 am
mso-number-format:"Long Time"    5:16:21:00
mso-number-format:"Percent"    Percent - two decimals
mso-number-format:"0%"    Percent - no decimals
mso-number-format:"0\.E+00"    Scientific Notation
mso-number-format:"\@"    Text
mso-number-format:"\#\ ???\/???"    Fractions - up to 3 digits (312/943)
mso-number-format:"\0022£\0022\#\,\#\#0\.00"    £12.76
mso-number-format:"\#\,\#\#0\.00_ \;\[Red\]\-\#\,\#\#0\.00\ "    

reference : http://www.netdominus.co.uk/knowledgebase/mod/resource/view.php?id=182


http://www.codersource.net/2010/02/09/exporting-data-grid-to-excel-using-c/


方法一:

在使用 DataGrid 匯出 EXCEL的方法 ,若數字開頭有 0 的話,匯出 EXCEL 都會EXCEL 自動移掉' ?% B" b; d. \9 C. }; w- s
例如:032546 匯出後會變成32546,所以這時候就要對 表格Style 設定一下,加入下面三行語法即可:

  1. string style = "<style> .text { mso-number-format:\\@; } </style> ";
  2. Response.Write(style);
  3. e.Item.Cells[0].Attributes.Add("class", "text");
=====================================================
  1.             Response.Clear();
  2.             Response.Buffer = true;
  3.  
  4.             string style = "<style> .text { mso-number-format:\\@; } </style> ";        //設定CSS樣式                                       
  5.  
  6.             Response.AddHeader("Content-Disposition", "attachment; filename=Remuneration.xls");
  7.             Response.ContentType = "application/vnd.ms-excel";
  8.             Response.Charset = "utf-8";
  9.  
  10.             EnableViewState = false;
  11.             StringWriter tw = new StringWriter();
  12.             HtmlTextWriter hw = new HtmlTextWriter(tw);                                               
  13.  
  14.             Datagrid2.RenderControl(hw);
  15.             Datagrid2.Visible = true;
  16.        
  17.             Response.Write(style);
  18.             Response.Write(tw.ToString());
  19.             Response.End();
  20.             Response.Close();

=====================================================

  1.         private void Datagrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  2.         {
  3.                 e.Item.Cells[0].Attributes.Add("class", "text");
  4.                 e.Item.Cells[1].Attributes.Add("class", "text");
  5.                 e.Item.Cells[2].Attributes.Add("class", "text");
  6.                 e.Item.BackColor = System.Drawing.Color.White;               
  7.         }

=====================================================

數字欄位強制轉文字 e.Row.Cells(1).Attributes.Add("style", "vnd.ms-excel.numberformat:@")

 

1.黑暗執行緒:TIPS-Export GridView To Excel
 
http://blog.darkthread.net/blogs/darkthreadtw/archive/2007/10/03/tips-export-gridview-to-excel.aspx
 
2.Export GridView to Excel(有程式碼可直接下載)
 
http://mattberseth.com/blog/2007/04/export_gridview_to_excel_1.html
 
3.將ASP.NET查詢結果(GridView)匯出為Excel檔
 
http://ms-net.blogspot.com/2008/04/aspnetgridviewexcel.html
 
4.[ASP.NET] GridView 轉 Excel
 
http://www.dotblogs.com.tw/dotjum/archive/2008/03/26/2289.aspx
 
5.ASP.NET 2.0服务器控件与form runat=server标记
 
http://blog.csdn.net/net_lover/archive/2006/09/25/1282106.aspx
 
6.關於GridView轉Excel
 
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20061121154011WCB&fumcde=FUM20041006161839LRJ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值