核心点
//针对集合1进行循环
<#list priceDetail as pd>//对集合1中包含的集合2进行存在和判空 判断<#if pd.detail ?exists && pd.detail ?size!=0> //对集合2进行循环<#list pd.detail as d>...</#list></#if></#list>
模版样式
语雀设计模版样式,导出为word,百度word转html文件,规范好编码格式Utf-8,对html文件进行修正后另存为.ftl文件即可使用.
导出效果
java代码
public String exportSmInspect(JSONObject priceDetail) throws Exception {Map<String, Object> map = new HashMap<>();String projectName = priceDetail.getString("projectName");//虚拟路径String fileNamePath = newProfile + DateUtils.datePath() + "/" + projectName + ".doc";//数据处理populateMapWithPriceDetail(map, priceDetail);//导出成wordexporttemplateword(map, "光伏报价模版.ftl", wordPath, projectName + "报价.doc");return fileNamePath;}
/*** 数据分析* 将传参循环存入map中** @param map* @param priceDetail*/private void populateMapWithPriceDetail(Map<String, Object> map, JSONObject priceDetail) {map.put("projectName", priceDetail.getString("projectName"));map.put("sunTime", priceDetail.getString("sunTime"));map.put("extraction", priceDetail.getString("extraction"));map.put("head", priceDetail.getString("head"));JSONArray detail = priceDetail.getJSONArray("priceDetail");if (!CollectionUtils.isEmpty(detail)) {List<Map<String, Object>> map1s = new ArrayList<>();for (Object o : detail) {if (o instanceof LinkedHashMap) {LinkedHashMap<String, Object> pds = (LinkedHashMap) o;Map<String, Object> map1 = new HashMap<>();map1.put("name", pds.get("name"));map1.put("totalPrice", pds.get("totalPrice"));map1.put("remark", pds.get("remark"));//匹配失败则没有详情if(pds.containsKey("detail")){Object detail1 = pds.get("detail");//转为集合List<?> list = Arrays.asList(detail1);if (!CollectionUtils.isEmpty(list)) {List<Map<String, Object>> map2s = new ArrayList<>();for (Object detail1Obj : list) {//查看实际类型-->注:debug显示未LinkedHashMap,判断为falseSystem.out.println(detail1Obj.getClass().getName());if (detail1Obj instanceof List) {ArrayList<LinkedHashMap> wlDetail = (ArrayList) detail1Obj;for (LinkedHashMap s : wlDetail) {Map<String, Object> map2 = new HashMap<>();map2.put("name", s.get("name"));map2.put("price", s.get("price"));map2.put("num", s.get("num"));map2.put("unit", s.get("unit"));map2s.add(map2);}}}map1.put("detail", map2s); }}else{//存入空Map,模版导出时进行判空跳过map1.put("detail",new HashMap<>());}map1s.add(map1);}}map.put("priceDetail", map1s);}}
/*** 导出为word文件** @param map 数据集合* @param path 模版名称* @param wordPath 模版所在的文件路径* @param name 文件名* @throws Exception*/public void exporttemplateword(Map<String, Object> map, String path, String wordPath, String name) throws Exception {// 第一步:创建一个Configuration对象,直接new一个对象。构造方法的参数就是FreeMarker对应的版本号。Configuration configuration = new Configuration(Configuration.getVersion());// 第二步:设置模板文件所在的路径,确保目录存在File file = new File(wordPath);if (!file.isDirectory()) {throw new IllegalArgumentException("word模板文件不存在,请检查word文件夹!" + wordPath);}configuration.setDirectoryForTemplateLoading(file);// 第三步:设置模板文件使用的字符集。一般就是utf-8.configuration.setDefaultEncoding("utf-8");// 第四步:加载一个模板,创建一个模板对象。Template template = configuration.getTemplate(path);// 第六步:创建一个Writer对象,一般创建一FileWriter对象,指定生成的文件名。//实际存储路径String saveFilePath = profile + DateUtils.datePath();File file1 = new File(saveFilePath);if (!file1.exists() && !file1.mkdirs()) {file1.mkdirs();}try (Writer out = new FileWriter(saveFilePath + "/" + name)) {// 第七步:调用模板对象的process方法输出文件。template.process(map, out);} catch (TemplateException e) {// 处理模板引擎异常throw new IOException("模板处理过程中发生错误", e);}}