C# GD匯出Excel

摘要:C# GD匯出Excel

@ 引用:程式基本寫法 

@ 引用: 小方法

 

方法一:

在使用 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