public void toexcel(System.Web.UI.WebControls.DataGrid datagrid)
{
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Write("<html xmlns:x=/"urn:schemas-microsoft-com:office:excel/">");
System.Web.HttpContext.Current.Response.Write("<head>");
System.Web.HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>");
System.Web.HttpContext.Current.Response.Write("<x:ExcelWorkbook>");
System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheets>");
System.Web.HttpContext.Current.Response.Write("<x:ExcelWorksheet>");
System.Web.HttpContext.Current.Response.Write("<x:Name>sheet1</x:Name>");
System.Web.HttpContext.Current.Response.Write("<x:WorksheetOptions>");
System.Web.HttpContext.Current.Response.Write("<x:Print>");
System.Web.HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>");
System.Web.HttpContext.Current.Response.Write("</x:Print>");
System.Web.HttpContext.Current.Response.Write("</x:WorksheetOptions>");
System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheet>");
System.Web.HttpContext.Current.Response.Write("</x:ExcelWorksheets>");
System.Web.HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
System.Web.HttpContext.Current.Response.Write("</xml>");
System.Web.HttpContext.Current.Response.Write("<![endif]--> ");
System.Web.HttpContext.Current.Response.Write("</head>");
System.Web.HttpContext.Current.Response.Write("<body>");
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.DateTime.Now.ToFileTime() + ".xls");
System.Web.HttpContext.Current.Response.Charset = "UTF-8";
System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";
//loggrid.Page.EnableViewState =false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
datagrid.RenderControl(hw);
System.Web.HttpContext.Current.Response.Write(tw.ToString());
System.Web.HttpContext.Current.Response.Write("<font style=/"color:white;/">.</font></body>");
System.Web.HttpContext.Current.Response.Write("</html>");
System.Web.HttpContext.Current.Response.End();
}
原帖子地址: http://forums.asp.net/1674188/ShowThread.aspx