JAVA实现Excel模板填充

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

需要将统计数据填充到指定的Excel模板中

 public void writeExcelFile(LocalDate start, LocalDate end, String filename) {
        File file = new File("files/report_Demo.xlsx");
        XSSFWorkbook workbook = null;
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            workbook = new XSSFWorkbook(fileInputStream);
        } catch (Exception e) {
        }
        //查找到指定的sheet
        XSSFSheet sheet = workbook.getSheet("抓取数据展示");
        sheet.setForceFormulaRecalculation(true);
        Map<String, List<StatInfo>> sourceCompares = getCompare(start, end);
        Map<String, String> catchMap = new HashMap<>();
        for (Map.Entry<String, List<StatInfo>> entry : sourceCompares.entrySet()) {
            String site = entry.getKey();
            int rowNum = 0;
           //判断指定数据存放到指定的行数
            switch (site) {
                case "weixin":
                    rowNum = 23;
                    break;
                case "article":
                    rowNum = 32;
                    break;
                case "rss":
                    rowNum = 43;
                    break;
                case "yidian":
                    rowNum = 54;
                    break;
                case "weiboarticle":
                    rowNum = 64;
                    break;
                case "toutiao":
                    rowNum = 75;
                    break;
            }
            XSSFRow row = sheet.getRow(rowNum);
            List<StatInfo> statInfos = entry.getValue();
            //按照相同的site的时间相同属性去重
            for (int i = 0; i < statInfos.size(); i++) {
                if (catchMap.containsKey(statInfos.get(i).getSite())
                        && catchMap.get(statInfos.get(i).getSite()).equals(statInfos.get(i).getTime())) {
                    statInfos.remove(i);
                } else {
                    catchMap.put(statInfos.get(i).getSite(), statInfos.get(i).getTime());
                }
            }
            for (int i = 0; i < statInfos.size(); i++) {
                //当上一周的数据填充完成了就填充本周7天数据,行数+1换到本周
                if (i == 7) {
                    row = sheet.getRow(rowNum + 1);
                }
                //从第三列开始进行,并以7天为一个周期循环找列数
                XSSFCell cell = row.getCell(3 + i % 7);
                cell.setCellType(XSSFCell.CELL_TYPE_NUMERIC);
                cell.setCellValue(statInfos.get(i).getCatchCount());
            }

        }
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        try {
            workbook.write(bos);
            File outfile = new File(filename);
            FileOutputStream fileOutputStream = new FileOutputStream(outfile);
            fileOutputStream.write(bos.toByteArray());
            fileOutputStream.flush();
            fileOutputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.println("周报数据已经完成,请检查");
    }

最终的Excel表格效果:

猜你喜欢

转载自blog.csdn.net/zhangvalue/article/details/89387585