1. 程式人生 > >今天發現了把DataGrid匯出Excel時格式化字串的方法。

今天發現了把DataGrid匯出Excel時格式化字串的方法。

{
   
this.Page.Response.Clear();
   
this.Page.Response.AddHeader("content-disposition""attachment;filename=excel.xls");
   
this.Page.Response.Charset ="";
   
//this.Page.Response.Cache.SetCacheability(System.Web.HttpCacheability.NoCache);
this.Page.Response.ContentType ="application/vnd.xls";

   System.IO.StringWriter stringWrite 
=new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter htmlWrite 
=new System.Web.UI.HtmlTextWriter(stringWrite);
   IList il 
= DisableControlColumns();
   
this.DataGrid1.RenderControl(htmlWrite);
   ShowControlColumns(il);
   
//stringWrite.WriteLine("<style>.text { mso-number-format:/@; } </style>");

this.Page.Response.Write(stringWrite.ToString());
   
this.Page.Response.End();
  }