2009年4月21日 星期二

DataGrid轉Excel

string ExcelHeader = "<html><head><meta http-equiv=Content-Type content=text/html; charset=utf-8><style>td{mso-number-format:\\@}</style></head><body>";
string ExcelFooter = "</body></html>";
Response.Clear();
Response.Buffer = true;
Response.AddHeader("content-disposition", "attachment;filename=new.xls");
Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
this.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(tw);
this.dgView.RenderControl(hw);
Response.Write(ExcelHeader + tw.ToString + ExcelFooter);
Response.End();

沒有留言:

張貼留言