JAVA导出Excel并弹出下载框

一、引言

JAVAWeb开发中导入导出Excel必不可少,本文介绍原始方法的导出,导入其他章节介绍

二、相关JAR包

 1 <!--导入导出  2021年5月9日10:22:55    yxp-->
 2         <dependency>
 3             <groupId>org.apache.poi</groupId>
 4             <artifactId>poi</artifactId>
 5             <version>3.16</version>
 6         </dependency>
 7         <dependency>
 8             <groupId>org.apache.poi</groupId>
 9             <artifactId>poi-ooxml</artifactId>
10             <version>3.16</version>
11         </dependency>

三、关键代码

 /**
     * yxp
     * 2021年6月16日15:32:57
     * 条件查询导出Excel
     * @param personnelQo 查询参数
     * @return
     */
    @ApiOperation(value = "条件查询导出Excel")
    @GetMapping("/excutePersonnelByExcelJN")
    public void excutePersonnelByExcelJN(PersonnelInfoQo qo, HttpServletResponse response) throws IOException {
        aaa(qo,response);
    }

    public void aaa(PersonnelInfoQo qo,HttpServletResponse res) throws IOException {
        /**
         * 以下为生成Excel操作
         */
        // 1.创建一个workbook,对应一个Excel文件
        HSSFWorkbook wb = new HSSFWorkbook();
        // 2.在workbook中添加一个sheet,对应Excel中的一个sheet
        HSSFSheet sheet = wb.createSheet("XXX表");
        // 3.在sheet中添加表头第0行,老版本poi对excel行数列数有限制short
        HSSFRow row = sheet.createRow((int) 0);
        // 4.创建单元格,设置值表头,设置表头居中
        HSSFCellStyle style = wb.createCellStyle();
        // 居中格式
        style.setAlignment(HSSFCellStyle.ALIGN_CENTER);

        // 设置表头
        HSSFCell cell = row.createCell(0);
        cell.setCellValue("小区名称");
        cell.setCellStyle(style);

        cell = row.createCell(1);
        cell.setCellValue("姓名");
        cell.setCellStyle(style);

        cell = row.createCell(2);
        cell.setCellValue("性别");
        cell.setCellStyle(style);

        cell = row.createCell(3);
        cell.setCellValue("年龄");
        cell.setCellStyle(style);

        cell = row.createCell(4);
        cell.setCellValue("身份证");
        cell.setCellStyle(style);

        cell = row.createCell(5);
        cell.setCellValue("电话号");
        cell.setCellStyle(style);

        cell = row.createCell(6);
        cell.setCellValue("住址");
        cell.setCellStyle(style);

        cell = row.createCell(7);
        cell.setCellValue("车牌号");
        cell.setCellStyle(style);

        cell = row.createCell(8);
        cell.setCellValue("人员类型");
        cell.setCellStyle(style);

        cell = row.createCell(9);
        cell.setCellValue("是否迁出");
        cell.setCellStyle(style);

        IPage<PersonnelInfoVo> personnelIPage = personnelInfoMapper.selectPageInfo(new Page<>(0, 8000), qo, qo.getScopeId(), qo.getScopeLevel());
        List<PersonnelInfoVo> lists = personnelIPage.getRecords();

        // 循环将数据写入Excel
        for (int i = 0; i < lists.size(); i++) {
            row = sheet.createRow((int) i + 1);
            PersonnelInfoVo list= lists.get(i);
            // 创建单元格,设置值
            row.createCell(0).setCellValue(list.getPlotName());
            row.createCell(1).setCellValue(list.getUserName());
            row.createCell(2).setCellValue(list.getSex());
            row.createCell(3).setCellValue(list.getAge());
            row.createCell(4).setCellValue(list.getIdCard());
            row.createCell(5).setCellValue(list.getPhone());
            row.createCell(6).setCellValue(list.getHouseAddress());
            row.createCell(7).setCellValue(list.getNo());
            row.createCell(8).setCellValue(list.getType());
            row.createCell(9).setCellValue(list.getEmigration());
        }

        
        //弹出下载
        String fileName = "XXX表";
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        wb.write(os);
        byte[] content = os.toByteArray();
        InputStream is = new ByteArrayInputStream(content);
        // 设置response参数,可以打开下载页面
        res.reset();
        res.setContentType("application/vnd.ms-excel;charset=utf-8");
        res.setHeader("Content-Disposition", "attachment;filename="
                + new String((fileName + ".xls").getBytes(), "iso-8859-1"));
        ServletOutputStream out = res.getOutputStream();
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;

        try {
            bis = new BufferedInputStream(is);
            bos = new BufferedOutputStream(out);
            byte[] buff = new byte[2048];
            int bytesRead;
            // Simple read/write loop.
            while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                bos.write(buff, 0, bytesRead);
            }
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        } finally {
            if (bis != null)
                bis.close();
            if (bos != null)
                bos.close();
        }
    }


  注意,如果顶部注解是@RestController前端是不会弹出下载框的,需要改为@Controller


//不弹出下载框,直接下载到本地
FileOutputStream out =new FileOutputStream("E:/XXX.xls"); wb.write(out); out.close();    
zzzzzzzzzzzz
原文地址:https://www.cnblogs.com/ymlyxp/p/14891538.html