栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

使用poi将数据导出到excel

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用poi将数据导出到excel

1.依赖
		
            org.apache.poi
            poi
            3.17
        
        
            org.apache.poi
            poi-ooxml
            3.17
        
2.后端代码
 @GetMapping("/writeExcel")
    public Result writeExcel() throws IOException {
        List list = borrowService.list(null);
        for (Borrow thisBorrow : list) {
            thisBorrow.setBook(bookService.getById(thisBorrow.getBid()));
            thisBorrow.setUser(userService.getById(thisBorrow.getUid()));
        }
        String fileName  = "C:\Users\26913\Desktop\报表"+new Date().getTime()+".xls";
        //创建一个簿
        Workbook workbook=new HSSFWorkbook();
        //创建一张表
        Sheet sheet = workbook.createSheet("报表");
        Row head = sheet.createRow(0);
        Cell cell = head.createCell(0);

        cell.setCellValue("序号");
        cell = head.createCell(1);
        cell.setCellValue("借阅人编号");
        cell = head.createCell(2);
        cell.setCellValue("借阅人姓名");
        cell = head.createCell(3);
        cell.setCellValue("借阅人电话");
        cell = head.createCell(4);
        cell.setCellValue("借阅人性别");
        cell = head.createCell(5);
        cell.setCellValue("图书编号");
        cell = head.createCell(6);
        cell.setCellValue("图书名称");
        cell = head.createCell(7);
        cell.setCellValue("借阅时间");
        cell = head.createCell(8);
        cell.setCellValue("归还时间");

        for (int i = 0; i < list.size(); i++) {
            Row row = sheet.createRow(i+1);
            row.createCell(0).setCellValue(i+1);
            row.createCell(1).setCellValue(list.get(i).getUid());
            row.createCell(2).setCellValue(list.get(i).getUser().getName());
            row.createCell(3).setCellValue(list.get(i).getUser().getPhone());
            row.createCell(4).setCellValue(list.get(i).getUser().getSex());
            row.createCell(5).setCellValue(list.get(i).getBid());
            row.createCell(6).setCellValue(list.get(i).getBook().getName());
            row.createCell(7).setCellValue(list.get(i).getBorrowTime());
            row.createCell(8).setCellValue(list.get(i).getStillTime());
        }
        FileOutputStream outputStream = new FileOutputStream(fileName);
        workbook.write(outputStream);
        //关闭流
        outputStream.close();
        return Result.ok().message("导出成功");
    }
3.效果展示

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1000338.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号