將DataGrid數(shù)據(jù)導(dǎo)出到Word文檔
private void ExportToWord_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.doc");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.word";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgDevice.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
將DataGrid數(shù)據(jù)導(dǎo)出到Excel文檔
private void ExportToExcel_Click(object sender, System.EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
dgDevice.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
本文為codeproject節(jié)選,原文地址為:http://www.codeproject.com/aspnet/DAtaGridExportToExcel.asp