23.POI导出

POI导出

XSSFWorkbook 对应Excel2007版本及以上

HSSFWorkbook 对应Excel2003版本

还要注意一点,不要用Swagger-ui测试导出的表格,这样的表格文件都是损坏的。

1.导入依赖

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>3.16</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-ooxml</artifactId>
    <version>3.16</version>
</dependency>

2.建表

在这里我有个习惯,就是喜欢把自己做好表头的Excel文件读取到程序中,然后再装填数据。

所以,我很少写更改表格样式的代码。

@Override
public XSSFWorkbook createMemberExcel(List idList, XSSFWorkbook excel) {
    List<Member> memberList= (List<Member>) this.listByIds(idList);
    int sheetNum = 0;
    XSSFSheet sheet = excel.getSheetAt(sheetNum);
    int rowNum = 1;
    for(Member member:memberList) {
        XSSFRow row = sheet.createRow(rowNum++);
        int cellNum = 0;
        XSSFCell cell = row.createCell(cellNum++);
        cell.setCellValue(member.getNum());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getName());
        cell = row.createCell(cellNum++);
        String sex = member.getSex()== true ? "女" : "男";
        cell.setCellValue(sex);
        cell = row.createCell(cellNum++);
        if(null != member.getBirth()) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            cell.setCellValue(member.getBirth().format(formatter));
        }
        cell = row.createCell(cellNum++);
        String type = member.getType()==true ? "教职工" : "学生";
        cell.setCellValue(type);
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getDep());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getPro());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getTeam());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getNational());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getOrigin());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getIdCard());
        cell = row.createCell(cellNum++);
        cell.setCellValue(member.getJoinOrganization());
        cell = row.createCell(cellNum++);
        if(null != member.getJoinDate()) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            cell.setCellValue(member.getJoinDate().format(formatter));
        }
    }
    return excel;
}

3.文件流导出

private void download(String filename, XSSFWorkbook excel,HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Expose-Headers","Content-Disposition");
        response.setHeader("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet.main+xml");
        response.setHeader("Content-Disposition", "attachment;filename="+ URLEncoder.encode(filename, "utf-8"));
        OutputStream outputStream = response.getOutputStream();
        excel.write(outputStream);
        outputStream.flush();
        outputStream.close();

        /*FileOutputStream fos = new FileOutputStream("1.xlxs");
        excel.write(fos);
        fos.flush();
        fos.close();*/
    }
posted @ 2019-01-21 14:51  零度微笑  阅读(154)  评论(0编辑  收藏  举报