DataGrid导出到Excel or word
//表头
string strTitle = this.LiteralTitle.Text;
//年月
string strYearMonth = this.LiteralDate.Text;
//填报人
string strUserInfo = this.LiteralUserInfo.Text;
Response.Clear();
Response.Buffer = true;
Response.Charset = "GB2312";
Response.AppendHeader("Content-Disposition", "online;filename=" + strYearMonth + strTitle + ".xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
Response.ContentType = "application/ms-excel";
//关闭 ViewState
this.EnableViewState = false ;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new HtmlTextWriter(tw);
//获取control的HTML
dgrdList.RenderControl(hw);
//把HTML写回浏览器
StringBuilder html = new StringBuilder();
html
.Append("<table cellspacing = \"0\" border = \"1\" >")
.Append("<tr align = Center style = \"height:24px;\"><td colSpan=\""+ ViewState["DgrdCellCount"].ToString() +"\">"+ strTitle +"</td></tr>")
.Append("<tr align = Center style = \"height:24px;\"><td colSpan=\""+ ViewState["DgrdCellCount"].ToString() +"\">"+ strYearMonth +"</td></tr>")
.Append("<tr align = left style = \"height:24px;\"><td colSpan=\""+ ViewState["DgrdCellCount"].ToString() +"\">"+ strUserInfo +"</td></tr>")
.Append("</table>");
Response.Write(html.ToString() + tw.ToString());
Response.End();
----------------------------------------------------------------
from:http://www.zxbc.cn/html/20080516/34394.html
protected void btnToExcel_Click(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "GB2312";
//假定导出的文件名为FileName.xls
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置导出文件的格式
Response.ContentType = "application/ms-excel";
//关闭ViewState
EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
gvPersonList.RenderControl(textWriter);
//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}
//导出成Word文件
protected void btnToWord_Click(object sender, EventArgs e)
{
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "GB2312";
//假定导出的文件名为FileName.doc
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.doc");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
////设置导出文件的格式
Response.ContentType = "application/ms-word";
//关闭ViewState
gvPersonList.EnableViewState = false;
System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter stringWriter = new System.IO.StringWriter(cultureInfo);
System.Web.UI.HtmlTextWriter textWriter = new System.Web.UI.HtmlTextWriter(stringWriter);
gvPersonList.RenderControl(textWriter);
// //把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}