Java 把数据库获取的数据 打印到Excel表格中

这两天写Excel导出,以前写过,但现在实在是想不起了,就翻了一下以前的项目代码,特此记录!

 
       //生成文件路径
        String toPath = rootPath + "cost/tmp/" + fileName;
        //创建workbook
        HSSFWorkbook wb = new HSSFWorkbook(new FileInputStream(new File(tempPath)));
        //cell风格、样式
        HSSFCellStyle cellStyle = wb.createCellStyle();
        //循环list数据,循环一次,写一行数据
        for (int i = 0; i < borrowDatList.size(); i++) {
            //创建行
            Row row = dataSheet.createRow(costLen + i + 1);
            //获取list数据,转为map
            Map map1 = (Map) borrowDatList.get(i);
            //转为entry
            Set<Map.Entry> entry = map1.entrySet();
            String[] dept_code = map1.get("dept_code").toString().split("-");
            map1.put("DEPT_CODE", dept_code[0]);
            //准备遍历
            Iterator<Map.Entry> ite = entry.iterator();
            int j = 0;
            //遍历数据,往Excel表格写数据
            while (ite.hasNext()) {
                Map.Entry entry1 = ite.next();
                entry1.getKey();
                String val = Tools.filterNull(entry1.getValue());
                Cell cell = row.createCell(j++);
                cell.setCellValue(val);
            }
        }
        FileOutputStream out = new FileOutputStream(toPath);
        wb.write(out);
        out.close();





  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值