apache poi 3.17 导出

快速导出代码

@RequestMapping("/exportJobDescription/list")
    public void exportJobDescription(HttpServletResponse response, String companyName, String jobName,String status,Integer type, String endTime, String startTime) throws Exception {
        Map<String, Object> param = new HashMap<>();
        param.put("companyName",companyName);
        param.put("endTime",endTime);
        param.put("status",status);
        param.put("type",type);
        param.put("jobName",jobName);
        param.put("startTime",startTime);
        List<JobDescriptionDto> list = jobDescriptionService.exportJobDescription(param);
        System.out.println(list.size());
        String[] header = {"序号", "企业名称", "岗位名称", "招聘人数", "工作地点", "发布时间", "结算时间","成交单价", "状态", "中标企业数","已完成人数"};
        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();
        //生成一个表格,设置表格名称
        HSSFSheet sheet = workbook.createSheet("招聘信息数据表");
 
        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);
        //创建第一行表头
        HSSFRow headRow = sheet.createRow(0);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            HSSFCell cell = headRow.createCell(i);
            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);
            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
        }
        //遍历查询结果
        for (int i = 0; i < list.size(); i++) {
            JobDescriptionDto b = list.get(i);
            HSSFRow row = sheet.createRow(i + 1);
            row.createCell(0).setCellValue(i+1);
            row.createCell(1).setCellValue(new HSSFRichTextString(b.getCompanyName()!=null?b.getCompanyName():""));
            row.createCell(2).setCellValue(new HSSFRichTextString(b.getJobName()!=null?b.getJobName():""));
            row.createCell(3).setCellValue(new HSSFRichTextString(b.getRecruitsNumber()!=null?b.getRecruitsNumber().toString():""));
            row.createCell(4).setCellValue(new HSSFRichTextString(b.getCityName()!=null?b.getCityName():""));
            row.createCell(5).setCellValue(new HSSFRichTextString(b.getReleaseTime()!=null? DateUtils.format(b.getReleaseTime(),"yyyy-MM-dd"):""));
            row.createCell(6).setCellValue(new HSSFRichTextString(b.getSettleTime()!=null? DateUtils.format(b.getSettleTime(),"yyyy-MM-dd"):""));
            row.createCell(7).setCellValue(new HSSFRichTextString(b.getPrice()!=null?b.getPrice().toString():""));
            String statusValue = "";
            if(b.getStatus()!=null){
                if (b.getStatus() == 0) {
                    statusValue = "草稿";
                } else if (b.getStatus() == 1) {
                    statusValue = "已付款待审核";
                } else if (b.getStatus() == 2) {
                    statusValue = "审核拒绝";
                } else if (b.getStatus() == 3) {
                    statusValue = "进行中";
                } else if (b.getStatus() == 4) {
                    statusValue = "招聘截止";
                } else if (b.getStatus() == 5) {
                    statusValue = "已全部结算";
                }
            }
            row.createCell(8).setCellValue(new HSSFRichTextString(statusValue));
            row.createCell(9).setCellValue(b.getBidNumber()!=null?b.getBidNumber().toString():"0");
            row.createCell(10).setCellValue(b.getCompleteNumber()!=null?b.getCompleteNumber().toString():"0");
        }
        String fileName=new String("招聘信息数据表".getBytes(),"ISO8859-1");
        response.setContentType("application/octet-stream; charset=UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename="+ fileName+".xls");
        response.addHeader("Pargam", "no-cache");
        response.addHeader("Cache-Control", "no-cache");
        //刷新缓冲
        response.flushBuffer();
        //workbook将Excel写入到response的输出流中,供页面下载
        workbook.write(response.getOutputStream());
    }

猜你喜欢

转载自www.cnblogs.com/ityangshuai/p/12690469.html