zoukankan      html  css  js  c++  java
  • 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);
        }

    //导出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"

    注意:在导出时,出现图片不能显示,记得要改为绝对路径

  • 相关阅读:
    对于数据的测试
    绕过前端,直接调用后端接口的可能性
    API接口自动化之3 同一个war包中多个接口做自动化测试
    API接口自动化之2 处理http请求的返回体,对返回体做校验
    API接口自动化之1 常见的http请求
    DB中字段为null,为空,为空字符串,为空格要怎么过滤取出有效值
    Linux 常用的压缩命令有 gzip 和 zip
    SQL 常用的命令
    JVM内存管理的机制
    Linux 常见命令
  • 原文地址:https://www.cnblogs.com/kinger906/p/3412564.html
Copyright © 2011-2022 走看看