java 将数据库数据插入excel表格中

public List<PkAccountManager> selectPkAccountManagerList() throws IOException {
        // 创建工作薄
        HSSFWorkbook workbook = new HSSFWorkbook();
        // 创建工作表
        HSSFSheet sheet = workbook.createSheet("sheet1");
        //创建首行名称
        HSSFRow name = sheet.createRow(0);
        name.createCell(0).setCellValue("经理id");
        name.createCell(1).setCellValue("战队名称");
        name.createCell(2).setCellValue("组数");

        //从数据库查数据
        List<PkAccountManager> pkAccountManagers = pkAccountManagerDao.selectPkAccountManagerList();

        for (int row = 0; row < pkAccountManagers.size(); row++) {
            //row+1从第二行开始插
            HSSFRow rows = sheet.createRow(row+1);
            PkAccountManager pkAccountManager = pkAccountManagers.get(row);

            // 向工作表中添加数据
            if (pkAccountManager.getManagerId() == null){
                rows.createCell(0).setCellValue("");
            }else{
                rows.createCell(0).setCellValue(pkAccountManager.getManagerId());

            }
            rows.createCell(1).setCellValue(pkAccountManager.getTeamName());

            if (pkAccountManager.getTeamCount() == null){
                rows.createCell(2).setCellValue("");
            }else{
                rows.createCell(2).setCellValue(pkAccountManager.getTeamCount());

            }

        }

        File xlsFile = new File("D:/poi.xls");
        FileOutputStream xlsStream = new FileOutputStream(xlsFile);
        workbook.write(xlsStream);
        return pkAccountManagers;
    }

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值