GridView, DataList数据导出Excel,Word

重写VerifyRenderingInServerForm(Control control),否则报错。具体原因还未知,Searching...

public override void VerifyRenderingInServerForm(Control control)
{

}

 

//导出到Excel
private void ExportToExcel()
{
Response.Clear();
Response.BufferOutput = true;
//设定输出的字符集
Response.Charset = "GB2312";
//假定导出的文件名为FileName.xls
Response.AppendHeader("Content-Disposition", "attachment;filename=SystemReport_" + DateTime.Now.ToString("yyyyMMddhhmm") + ".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);
textWriter.WriteLine("<p style='font-family:Vendan;font-size:18px;font-weight:bold;color:Blue;'>Activity Config</p>");
DataList1.RenderControl(textWriter);
textWriter.WriteLine("<br /><br />");

textWriter.WriteLine("<p style='font-family:Vendan;font-size:18px;font-weight:bold;color:Blue;'>DMS Issue List</p>");
GridView1.RenderControl(textWriter);

//把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}

public override void VerifyRenderingInServerForm(Control control)
{

}


private void ExportToWord()
{
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
GridView1.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);
DataList1.RenderControl(textWriter);
// //把HTML写回浏览器
Response.Write(stringWriter.ToString());
Response.End();
}

 


 

posted @ 2011-12-12 16:51  eva.xiao  阅读(423)  评论(0编辑  收藏  举报