Java 导出接口(poi)


1.在pom.xml中添加poi依赖

<!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.17</version>
        </dependency>

2.主要代码

(1)controller层

 /**
     * Excel表格导出接口
     * http://localhost:端口号/ExcelDownload
     * @param response response对象
     * @throws IOException 抛IO异常
     */
    @ApiOperation("导出接口")
    @PostMapping("/ExcelDownload")
    public void excelDownload(HttpServletResponse response) throws IOException {
        uploadService.export(response);
    }

(2)service层

public void export(HttpServletResponse response){
        //表头数据
        String[] header = {"主键", "姓名", "生日", "创建时间", "母亲", "学校","年龄"};

        //声明一个工作簿
        HSSFWorkbook workbook = new HSSFWorkbook();

        //生成一个表格,设置表格名称为"学生表"
        HSSFSheet sheet = workbook.createSheet("学生表");

        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);
        //创建标题的显示样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //创建第一行表头
        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);
            cell.setCellStyle(headerStyle);
        }
        //获取所有的student数据
        List<Student> emps=uploadMapper.getStudentData();
        for(int i=0;i<emps.size();i++){
            //创建一行
            HSSFRow row1 = sheet.createRow(i+1);
            //第一列创建并赋值
            row1.createCell(0).setCellValue(new HSSFRichTextString(emps.get(i).getId().toString()));
            //第二列创建并赋值
            row1.createCell(1).setCellValue(new HSSFRichTextString(emps.get(i).getName()));
            //第三列创建并赋值
            row1.createCell(2).setCellValue(new HSSFRichTextString(emps.get(i).getBirthday()));
            //第四列创建并赋值-最高学历
            row1.createCell(3).setCellValue(new HSSFRichTextString(emps.get(i).getCreateTime()));
            //第五列创建并赋值-最高学历
            row1.createCell(4).setCellValue(new HSSFRichTextString(emps.get(i).getMother()));
            //第六列创建并赋值-最高学历
            row1.createCell(5).setCellValue(new HSSFRichTextString(emps.get(i).getSchool()));
            //第七列创建并赋值-最高学历
            row1.createCell(6).setCellValue(new HSSFRichTextString(String.valueOf(emps.get(i).getAge())));
        }
        //准备将Excel的输出流通过response输出到页面下载
        //八进制输出流
        response.setContentType("application/octet-stream");
        //这后面可以设置导出Excel的名称,此例中名为student.xls
        response.setHeader("Content-disposition", "attachment;filename=employee.xls");
        try {
            //刷新缓冲
            response.flushBuffer();
            //workbook将Excel写入到response的输出流中,供页面下载
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

3.在postman中进行测试(http://localhost:端口号/ExcelDownload)

3.数据导出结果

  final:不积跬步,无以至千里.不积小流,无以成江海


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM