HTML页面的导出,包括Excel和Word导出
//导出到Excel --- 全部导出,可以设置一些隐藏进行导出
protected void btnExport_Click(object sender, EventArgs e)
{
div_table.InnerHtml = hfdHtml.Value;//将页面内容重新放回去,因为后台按钮会冲掉已经生成的页面
btnExport.Visible = false;//导出按钮设置为不可见
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
}
{
div_table.InnerHtml = hfdHtml.Value;//将页面内容重新放回去,因为后台按钮会冲掉已经生成的页面
btnExport.Visible = false;//导出按钮设置为不可见
string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ClearHeaders();
System.Web.HttpContext.Current.Response.Buffer = false;
System.Web.HttpContext.Current.Response.ContentType = "application/octet-stream";
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName);
}
//导出word操作,本实例可以导出html样式
protected void btnExport_Click(object sender, EventArgs e) { divAdd.Visible = false;//需要隐藏的div string strFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".doc"; Response.Buffer = true; System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + strFileName); Response.ContentType = "application/ms-word"; HttpContext.Current.Response.Charset = "UTF-8"; this.EnableViewState = false;//初始化HtmlWriter System.IO.StringWriter writer = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer); this.RenderControl(htmlWriter);//输出 HttpContext.Current.Response.Write(writer.ToString()); HttpContext.Current.Response.End(); }
另外在前台要加上 ValidateRequest="false" EnableEventValidation="false"
注意:在导出时,出现图片不能显示,记得要改为绝对路径
posted on 2013-11-07 15:24 kingtiger 阅读(5280) 评论(0) 编辑 收藏 举报