• .net导出excle无需任何插件,直接通过一个tablehtml实现


    项目背景:

      项目需要导出样式复杂的excl表格,主要是一些样式布局比较复杂

    技术分析:

      目前比较通用的实现方式有

        1、借助微软的excle插件

        2、通过NPOI插件实现

        3、直接导出一个html(table),在输出头部指定文件输出类型

      本次主要说的第3中实现方式,这样实现起来很方便,尤其是针对样式复杂的表格,就当写html一样可以实现复杂的显示逻辑

      直接替代吗

    后端:采用的一个一般处理程序:

      

    namespace WebApplication3 {
        /// <summary>
        /// DownLoadExcle 的摘要说明
        /// </summary>
        public class DownLoadExcle : IHttpHandler {
    
            public void ProcessRequest(HttpContext context) {
    
                context.Response.ClearHeaders();
                context.Response.Clear();
                context.Response.AppendHeader("content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(System.DateTime.Now.ToString("yyyyMMddHHmmss")+ ".xls", Encoding.UTF8));
                // 在此处放置用户代码以初始化页面
                context.Response.ContentType = "application/vnd.ms-excel";
                // 从Content-Type header中去除charset设置
                context.Response.ContentEncoding = Encoding.UTF8;//.GetEncoding("GB2312")  .UTF8;//解决中文乱码之关键
                // 关闭 ViewState
                //EnableViewState = false;
                // 把HTML写回浏览器
                context.Response.Write("<meta http-equiv="content-type" content="application/ms-excel; charset=UTF-8"/>");
                context.Response.Write(this.GetExcleDataHtml());
                context.Response.Flush();
                context.Response.Clear();
                context.Response.End();
            }
    
            public bool IsReusable {
                get {
                    return false;
                }
            }
    
            /// <summary>
            ///  获取需要导出的数据HTML字符串
            /// </summary>
            /// <returns></returns>
            private string GetExcleDataHtml() {
                StringBuilder sbReport = new StringBuilder();
                sbReport.Append("<table border='1' cellpadding='0' cellspacing='0'>");
                sbReport.Append("<tr>");
                sbReport.Append("<th ><h4>Id</h4></th>");
                sbReport.Append("<th style='200px;'><h4>姓名</h4></th>");
                sbReport.Append("<th style='100px;'><h4>成绩</h4></th>");
                sbReport.Append("</tr>");
    
                for (int i = 0; i < 10; i++) {
                    sbReport.Append("<tr>");
                    sbReport.Append(string.Format("<td>{0}</td>", i + 1));
                    sbReport.Append(string.Format("<td>测试{0}</td>", i + 1));
                    sbReport.Append(string.Format("<td>{0}</td>", (i + 1) * 10));
                    sbReport.Append("</tr>");
                }
                sbReport.Append("</table>");
                return sbReport.ToString();
            }
        }
    }
    

      

    前端代码:直接用的一个a标签,链接到下载的一般处理程序地址即可

    <!DOCTYPE html>
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <title></title>
        <meta charset="utf-8" />
    </head>
    <body>
        <a href="DownLoadExcle.ashx">下载文件</a>
    </body>
    </html>
  • 相关阅读:
    linux常用命令
    ANAFI EXTENOED无人机(1)环境配置和基础开发
    无人机自主降落
    ROS开发(1)安装环境
    bebop无人机(1)环境配置和基础开发
    YOLO标注软件
    Python2与Python3之间切换
    python实现IOU计算
    读取多个(海康大华)网络摄像头的视频流 (使用opencv-python),解决实时读取延迟问题
    如何到外面的世界看看
  • 原文地址:https://www.cnblogs.com/xiaoXuZhi/p/downloadExcle.html
Copyright © 2020-2023  润新知