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

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_29622845/article/details/81063888

这两天写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();





猜你喜欢

转载自blog.csdn.net/qq_29622845/article/details/81063888