摘要:C# GD匯出Excel
@ 引用:程式基本寫法
@ 引用: 小方法
方法一:
在使用 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.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