简单的poi导出Excle

    //制定Excle标题和首列模板  参数: 模板url和 list
    public void downExcle(HttpServletResponse response, String tempPath, List<Map<String, Object>> list)
            throws Exception {
        // 1.根据模板创建Workbook对象
        File f1 = new File(tempPath);
        if (!tempPath.endsWith(".xlsx")) {
            LOGGER.info("downExcle模板不符合规范");
            return;
        }
        XSSFWorkbook wb = (XSSFWorkbook) WorkbookFactory.create(f1);
        // 2.向Workbook对象中继续添加内容
        XSSFSheet sheet = wb.getSheetAt(0);
        int i = 2;
        for (Map<String, Object> map : list) {// map中value泛型不同
            XSSFRow row = sheet.createRow(i);
            String c0 = (String) map.get("NAME");
            row.createCell(0).setCellValue(c0);
            BigDecimal c1 = (BigDecimal) map.get("PRJ_INVESTMENT");
            row.createCell(1).setCellValue(c1.toString());
            Long c2 = (Long) map.get("PRJ_NUM");
            row.createCell(2).setCellValue(c2);
            i++;
        }

        OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
        response.reset();
        String fileName = URLEncoder.encode("统计报表" + ".xlsx", "UTF-8");
        response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        response.setContentType("application/msexcel;charset=UTF-8");
        wb.write(outputStream);
        outputStream.flush();
        wb.close();
        response.flushBuffer();
    }

猜你喜欢

转载自blog.csdn.net/qq_41859067/article/details/84473896