public void doExport(HttpServletResponse response, HttpServletRequest request,
                         StatisticsSearchModel searchModel) { id);
        String title = "检查情况统计"; 
        JSONArray jsonArr = new JSONArray();
        getTableResultJson(searchModel,jsonArr);
        //获取检查类型
        Map<String, List<TCheckType>> result =  OutMatchTaskServiceImpl.getAllCheckTypeMap();
        //开始拼装Excel表头
        List<ExcelExportEntity> entity = new ArrayList<ExcelExportEntity>();
        ExcelExportEntity excelentityNo = new ExcelExportEntity("序号", "No");
        excelentityNo.setNeedMerge(true);
        entity.add(excelentityNo);
        ExcelExportEntity excelExportEntity0 = new ExcelExportEntity("收样时间", "");
        excelExportEntity0.setNeedMerge(true);
        entity.add(excelExportEntity0);
        ExcelExportEntity excelExportEntity1 = new ExcelExportEntity("检查单位(TA)", "entrustUnit");
        excelExportEntity1.setNeedMerge(true);
        entity.add(excelExportEntity1);
        ExcelExportEntity excelExportEntity2 = new ExcelExportEntity("检查类别", "typeName");
        excelExportEntity2.setNeedMerge(true);
        entity.add(excelExportEntity2);
        ExcelExportEntity excelExportEntity3 = new ExcelExportEntity("检查类型", "types");
        excelExportEntity3.setNeedMerge(true);
        List<ExcelExportEntity> entity1 = new ArrayList<>();
        List<TCheckType> urine1= result.get("urine");
        for (TCheckType x:urine1){
            ExcelExportEntity excel = new ExcelExportEntity(x.getCnName(), "nj_"+x.getId());
            excel.setStatistics(true);
            entity1.add(excel);
        }
        List<TCheckType> blood1= result.get("blood");
        for (TCheckType y:blood1){
            ExcelExportEntity excel1 = new ExcelExportEntity(y.getCnName(), "xj_"+y.getId());
            excel1.setStatistics(true);
            entity1.add(excel1);
        }
        excelExportEntity3.setList(entity1);
        entity.add(excelExportEntity3);
        ExcelExportEntity excelExportEntity4 = new ExcelExportEntity("备注", "remarks");
        excelExportEntity4.setNeedMerge(true);
        entity.add(excelExportEntity4);
        int x = jsonArr.size();
        //进行检查类型拼装
        for (int i = 0; i < jsonArr.size(); i++) {
            JSONObject as  =  jsonArr.getJSONObject(i);
            List<Map<String,Object>>list1 = new ArrayList(); //检查类型
            Map<String,Object>map1 = new HashMap<>(); //收集检查类型的具体类型
            for (String a :as.keySet()) {
                if (a.split("nj_").length>1){
                    map1.put(a,as.get(a));
                }else if (a.split("xj_").length>1){
                    map1.put(a,as.get(a));
                }
            }
            list1.add(map1);
            as.put("types",list1);
        }
        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(title, "送样统计"),
                entity, jsonArr);
        Sheet sheet = workbook.getSheetAt(0);//取此Excel文件的第一个Sheet
        // ========================= 对合并行进行去小数点 ==================
        Row rowEnd = sheet.getRow( sheet.getLastRowNum()); //获取到最后一个单元格
        CellStyle cellStyle =workbook.createCellStyle();
        DataFormat format =  workbook.createDataFormat();
        cellStyle.setDataFormat(format.getFormat("@"));
        rowEnd.setRowStyle(cellStyle);
        for (int i = 0; i <rowEnd.getLastCellNum() ; i++) {
            try {
                Cell j =  rowEnd.getCell(i);
                String k =j.getStringCellValue();
                if (!StringUtils.isEmpty(k)){
                    String []klist= k.split("\\.");
                    if (klist.length>=2){
                        j.setCellValue( klist[0]);
                    }
                }
            }catch (NullPointerException e){
            }
        }
        //四个参数依次是:起始行,终止行,起始列,终止列
        CellRangeAddress craOne = new CellRangeAddress(x+3, x+3, 0, 3);//index从0开始
        sheet.addMergedRegion(craOne); //第一次合并
        downLoadExcel("送样统计.xls",response,request,workbook);
        LogManageUtils.recordLog("送样统计","/statistics/doExportSearchSampleStatistics","送样统计",
                UserCommon.getUserId(),UserCommon.getRealName(),
                null,null, ConstantsUtils.LOG_LEVEL_INFO);
    }
    /**
     * 实现Excel下载
     * @param fileName
     * @param response
     * @param workbook
     */
    private static void downLoadExcel(String fileName, HttpServletResponse response,HttpServletRequest request, Workbook workbook) {
        try {
            String browser = "";
            browser = request.getHeader("User-Agent");
            if (-1 < browser.indexOf("MSIE 6.0") || -1 < browser.indexOf("MSIE 7.0")) {
                // IE6, IE7 浏览器
                response.addHeader("content-disposition", "attachment;filename="
                        + new String(fileName.getBytes(), "ISO8859-1"));
            } else if (-1 < browser.indexOf("MSIE 8.0")) {
                // IE8
                response.addHeader("content-disposition", "attachment;filename="
                        + URLEncoder.encode(fileName, "UTF-8"));
            } else if (-1 < browser.indexOf("MSIE 9.0")) {
                // IE9
                response.addHeader("content-disposition", "attachment;filename="
                        + URLEncoder.encode(fileName, "UTF-8"));
            } else if (-1 < browser.indexOf("Chrome")) {
                // 谷歌
                response.addHeader("content-disposition",
                        "attachment;filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8"));
            } else if (-1 < browser.indexOf("Safari")) {
                // 苹果
                response.addHeader("content-disposition", "attachment;filename="
                        + new String(fileName.getBytes(), "ISO8859-1"));
            } else {
                // 火狐或者其他的浏览器
                response.addHeader("content-disposition",
                        "attachment;filename*=UTF-8''" + URLEncoder.encode(fileName, "UTF-8"));
            }
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw  new ExcelExportException(e.getMessage());
        }
    }
                poi 动态导出excel数据
原创
             ©著作权归作者所有:来自51CTO博客作者小飞侠格鲁帅的原创作品,请联系作者获取转载授权,否则将追究法律责任        
             
            
        
                提问和评论都可以,用心的回复会被更多人看到
                评论
            
            
                    发布评论
                
            
            相关文章
        
        
- 
                    POI导出Excel表格体需要的jar包参考上篇文章 "POI解析Excel表格" 创建表格的格式内容是 字符串数组。 运行测试类,生成 表格 导出结束,谢谢观赏! POI编程 apache java excel表格
- 
                    java poi 导出excel在web开发中,有一个经典的功能,就是数据的导入导出。特别 java dataset string excel date
- 
                    Java使用poi导入导出Excel java poi导出excel相信在大部分的web项目中都会有导出导入Excel的需求,今天我们就来看看如何用Java代码去实现 用POI导出Excel表格。之前我也写过两篇导出Excel的文章,没看过的小伙伴可以去看哈,链接也给大家放出来了:1.导出多个sheet2.导出zip压缩包一、pom引用pom文件中,添加以下依赖< Java使用poi导入导出Excel java 前端 spring boot spring cloud
 
 
                    













 
                    

 
                 
                    