poi导出Excel

 查询使用的是mybaits-plus

1.添加jar包

  poi
  poi-ooxml
  poi-ooxml-schemas
  poi-scratchpad
  poi-contrib

2.前台传递过来的是一个String类型的字符串是勾选的id

3.Controller层的代码

    private static final String XLSX = "xlsx";
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public String export(HttpServletResponse response, String feedBackIds) {
        String[] ids = feedBackIds.split(",");
        try {
            response.reset();
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            response.setHeader("Content-Type", "application/force-download");
            response.setHeader("Content-Tpye", "application/vnd.ms-excel");
            String fileName = "xxx表."+ XLSX;
            response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
            ServletOutputStream outputStream = response.getOutputStream();
            feedBackQueryService.export(outputStream, ids);
            return "成功";
        } catch (IOException e) {
            e.printStackTrace();
            return "失败";
        }
    }

4.service层代码

 public void export(ServletOutputStream outputStream, String[] feedBackIds) {
        try {
            Workbook workbook = new XSSFWorkbook();
            Sheet sheet = workbook.createSheet();

            CellStyle style1 = workbook.createCellStyle();
            // 设置为文本格式,防止变成科学计数法
            DataFormat format = workbook.createDataFormat();
            style1.setDataFormat(format.getFormat("@"));

            Row row = sheet.createRow(0);
            row.createCell(0).setCellValue("a");
            row.createCell(1).setCellValue("b");
            row.createCell(2).setCellValue("c");
            row.createCell(3).setCellValue("d");
            row.createCell(4).setCellValue("e");
            row.createCell(5).setCellValue("f");
            row.createCell(6).setCellValue("g");
            row.createCell(7).setCellValue("h");
            row.createCell(8).setCellValue("i");
            row.createCell(9).setCellValue("j");
            row.createCell(10).setCellValue("k");
            row.createCell(11).setCellValue("l");

            List yjxCjFkInfoDOS = baseMapper.selectList(new QueryWrapper().in("id", feedBackIds));
            List yjxCjFkInfoVOS = convertVO(yjxCjFkInfoDOS);
            int j = 0;
            for (YjxCjFkInfoVO yjxCjFkInfoVO : yjxCjFkInfoVOS) {
                j++;
                Row salaryRow = sheet.createRow(j);
                translateSalary(salaryRow, yjxCjFkInfoVO);
            }
            workbook.write(outputStream);
            outputStream.flush();
            outputStream.close();


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 private boolean translateSalary(Row createRow, YjxCjFkInfoVO yjxCjFkInfoVO) {
        if (yjxCjFkInfoVO == null) {
            return true;
        }
        createRow.createCell(0).setCellValue(yjxCjFkInfoVO.getId());
        createRow.createCell(1).setCellValue(yjxCjFkInfoVO.getCompanyname());
        createRow.createCell(2).setCellValue(yjxCjFkInfoVO.getCompanycode());
        createRow.createCell(3).setCellValue(yjxCjFkInfoVO.getUsercode());
        createRow.createCell(4).setCellValue(yjxCjFkInfoVO.getUsername());
        createRow.createCell(5).setCellValue(yjxCjFkInfoVO.getFeedbacklatitudetext());
        createRow.createCell(6).setCellValue(yjxCjFkInfoVO.getRiskcase());
        createRow.createCell(7).setCellValue(yjxCjFkInfoVo.getRiskcase());
        createRow.createCell(8).setCellValue(nextOption(yjxCjFkInfoVO.getNextoperation()));
        createRow.createCell(9).setCellValue(yjxCjFkInfoVO.getFeedbackcontent());
        createRow.createCell(10).setCellValue(new SimpleDateFormat("yyyy-MM-dd").format(yjxCjFkInfoVO.getFeedbacktime()));
        createRow.createCell(11).setCellValue(yjxCjFkInfoVO.getPagesearchcode());
        return false;
    }
或参考:https://blog.csdn.net/liberty12345678/article/details/82109553

 

你可能感兴趣的:(poi导出Excel)