zoukankan      html  css  js  c++  java
  • 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();*/
        }
    
  • 相关阅读:
    iOS开发时,在Xcode中添加多个Targets进行版本控制
    如何给苹果公司发邮件?
    快快快!27个提升效率的iOS开源库推荐
    史上最全的常用iOS的第三方框架
    Ajax提交与传统表单提交的区别说明
    js原生ajax请求get post笔记
    查看iOS Crash logs
    如何提高iOS开发能力
    iOS 常用的#define合集
    Xcode编译错误和警告汇总
  • 原文地址:https://www.cnblogs.com/TimerHotel/p/springboot23.html
Copyright © 2011-2022 走看看