日常技术点记录-one

生成word文档

serviceImpl层
@Override
    public void genAddressInfoReport(BigDecimal longitude, BigDecimal latitude, Integer busNum, String address) {
        BusStationSelectAddressVo selectAddressInfo = getSelectAddressInfo(longitude, latitude, busNum);
        /**
         * 生成word文档
         */
        if (selectAddressInfo == null) {
            throw new RuntimeException("数据为空");
        }
        List<SelectAddressBusVo> busVoList = selectAddressInfo.getList();
        if(CollectionUtil.isEmpty(busVoList)){
            throw new RuntimeException("无调整线路明细");
        }
        long lineCount = busVoList.stream().map(b -> b.getLineName()).distinct().count();
        // 初始化数据map
        Map<String, Object> dataMap = new HashMap<>();
        // 录入采购基本数据
        dataMap.put("address", address);
        dataMap.put("lineNum", lineCount);
        dataMap.put("busNum", busVoList.size());
        dataMap.put("saveDistance",selectAddressInfo.getDistanceTotal());
        dataMap.put("saveEnergy",selectAddressInfo.getPowerTotal());
        dataMap.put("saveDailyTotal",selectAddressInfo.getDayTotal());
        dataMap.put("saveMonthTotal",selectAddressInfo.getMonthTotal());
        dataMap.put("saveYearTotal", selectAddressInfo.getYearTotal());
        dataMap.put("list",busVoList);
        //Configuration 用于读取ftl文件
        //new Configuration()构造方法内是当前依赖的版本号
        try {
            Configuration cfg = new Configuration();
            cfg.setDirectoryForTemplateLoading(new File("D:\\sc_energy_consumption\\energy_consumption_service\\src\\main\\resources\\templates"));
            //输出文档路径及名称
            File outFile = new File("D:\\testFile\\okk.doc");
            //以utf-8的编码读取ftl文件
            Template template = cfg.getTemplate("xzbg.ftl","UTF-8");
            Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
            template.process(dataMap, out);
            out.close();
        }catch (IOException e){
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
    }