利用網頁轉出資料到 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\ "
=====================================================
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 設定一下,加入下面三行語法即可:
- string style = "<style> .text { mso-number-format:\\@; } </style> ";
- Response.Write(style);
- e.Item.Cells[0].Attributes.Add("class", "text");
- Response.Clear();
- Response.Buffer = true;
- string style = "<style> .text { mso-number-format:\\@; } </style> "; //設定CSS樣式
- Response.AddHeader("Content-Disposition", "attachment; filename=Remuneration.xls");
- Response.ContentType = "application/vnd.ms-excel";
- Response.Charset = "utf-8";
- EnableViewState = false;
- StringWriter tw = new StringWriter();
- HtmlTextWriter hw = new HtmlTextWriter(tw);
- Datagrid2.RenderControl(hw);
- Datagrid2.Visible = true;
- Response.Write(style);
- Response.Write(tw.ToString());
- Response.End();
- Response.Close();
=====================================================
- private void Datagrid2_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
- {
- e.Item.Cells[0].Attributes.Add("class", "text");
- e.Item.Cells[1].Attributes.Add("class", "text");
- e.Item.Cells[2].Attributes.Add("class", "text");
- e.Item.BackColor = System.Drawing.Color.White;
- }
=====================================================
數字欄位強制轉文字 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.youkuaiyun.com/net_lover/archive/2006/09/25/1282106.aspx
6.關於GridView轉Excel
http://www.blueshop.com.tw/board/show.asp?subcde=BRD20061121154011WCB&fumcde=FUM20041006161839LRJ