• poi 导出Excel --实战代码(导出一行过一排排那种/工具类导出/复制代码用)--历史保留版,可看最新版


    html代码

       <a class="btn btn-primary fa fa-export" href="javascript:void(0);" onclick="expExcel()"><span>导出excel</span></a>

    js代码

    function expExcel() {
            
            var ids=new Array();
            $("input:checkbox:checked").each(function(){
                ids.push($(this).attr("id"));
            });
            if(ids.length==0){
                alert("请至少选中一条记录");
                return;
            }
            if (confirm("导出选中页面数据?")) {        
                var url = __ctx + '/platform/xxx/xxx/exportExcel.htm?myId='+ ids;
                var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串
                if (userAgent.indexOf("compatible") > -1
                        && userAgent.indexOf("MSIE") > -1) {//判断是否IE浏览器
                    window.location.href(url);
                } else {
                    window.open(url, "导出报表");
                }
            }
        }

    java代码1

    @RequestMapping({ "exportExcel" })
        public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
            String[] aryIds = RequestUtil.getStringAryByStr(request, "myId");
            QueryFilter queryFilter = getQuerFilter(request);
            String myIds = "";
    
            for (int i = 0; i < aryIds.length; i++) {
                String tmp = aryIds[i].replace("jqg_jzjxjhGrid_", "");
    
                if (i != aryIds.length - 1) {
                    myIds += "'" + tmp + "',";
                } else {
                    myIds += "'" + tmp + "'";
                }
            }
            queryFilter.addParamsFilter("whereSql", "MY_ID_ in (" + myIds + ")");
            queryFilter.addParamsFilter("orderBySql", "KS ASC");
    
            List<Jzjxjh> query = jzjxjhManager.query(queryFilter);
    
            String title = "xxx计划";
            String[] headers = {"序号","xx","xx","xx","开始","结束","工期","xx","发电类型"};
            Integer[] columnWidth={18,18,18,18,18,18,18,18,18};
            
            String[][] values = new String[query.size()][headers.length];
            for (int i = 0; i < query.size(); i++) {
                Jzjxjh e = query.get(i);
                values[i][0]=e.getXh()+"";
                values[i][1]=e.getDw();
                values[i][2]=e.getJz();
                values[i][3]=e.getRl()+"";
                values[i][4]=sdf.format(e.getKs());
                values[i][5]=sdf.format(e.getJs());
                values[i][6]=e.getGq()+"";
                values[i][7]=e.getLx();
                values[i][8]=e.getZt();
            }
            
            HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(title, headers, values,columnWidth);        
            
            String fileName = "xxxx计划_" + ".xls";
            ExportExcelUtil.exportExcel(request, response, wb, fileName);
        
        }

     java代码2(跟上面1差不多,优点用到了反射类工具类,不用一个个get实体类的数据方便快捷,该工具类也可以从本博客util标签中找)

    @RequestMapping({ "exportExcel" })
        public void exportExcel(HttpServletRequest request, HttpServletResponse response) throws Exception {
            String[] aryIds = RequestUtil.getStringAryByStr(request, "myId");
            QueryFilter queryFilter = getQuerFilter(request);
            String myIds = "";
    
            for (int i = 0; i < aryIds.length; i++) {
                String tmp = aryIds[i].replace("jqg_jzjxjhGrid_", "");
    
                if (i != aryIds.length - 1) {
                    myIds += "'" + tmp + "',";
                } else {
                    myIds += "'" + tmp + "'";
                }
            }
            queryFilter.addParamsFilter("whereSql", "MY_ID_ in (" + myIds + ")");
            queryFilter.addParamsFilter("orderBySql", "KS ASC");
    
            List<Jzjxjh> query = jzjxjhManager.query(queryFilter);
    
            String title = "xxx计划";
            String[] headers = {"序号","单位","机组","容量","开始","结束","工期","类型","发电类型"};
            Integer[] columnWidth={18,18,18,18,18,18,18,18,18};
            String[] headersVar = {"xh","dw","jz","rl","ks","js","gq","lx","zt"};
            
            String[][] values = new String[query.size()][headers.length];
            for (int i = 0; i < query.size(); i++) {
                Jzjxjh obj = query.get(i);
                for (int j = 0; j < headersVar.length; j++) {
                    values[i][j]=ReflectUtil.getStringValue(headersVar[j], obj, "yyyy/MM/dd");
                }
                
            }
            
            HSSFWorkbook wb = ExportExcelUtil.getHSSFWorkbook(title, headers, values,columnWidth);        
            
            String fileName = "xxx计划_" + ".xls";
            ExportExcelUtil.exportExcel(request, response, wb, fileName);
        
        }

    适用场景:poi 4.0.1  ,ExportExcelUtil.java可以在本博客util中找

    导出效果

    第一行是合并单元格的一个标题

    第二行是表头

    第三行开始每一列都是数据

    效果图如下

  • 相关阅读:
    关于Adobe CS3套装的"此产品的许可已停止" 的解决方案
    Activity的生命周期 二
    APICS与AX的Master Planning(二)Rescheduling Assumption 重排假设
    COMVariantType的Date类型
    APICS与AX的Master Planning(一)Phantom bill of Material 虚项
    APICS与AX的Master Planning(四)Time Fence时限(时界)
    APICS与AX的Master Planning(三)Firm Planned Orders已确认计划订单
    设定Access数据库自增长字段初始
    SQL Server数据库导入导出数据方式比较
    个性博客七款超靓天气预报免费代码
  • 原文地址:https://www.cnblogs.com/rdchen/p/13999056.html
Copyright © 2020-2023  润新知