导出word我们常用的是通过POI实现导出。POI最擅长的是EXCEL的操作。word操作起来样式控制还是太繁琐了。今天我们介绍下通过FREEMARK来实现word模板导出。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>
${}
进行占位就行了。我们导出的时候提供相应的数据就行了。这里注意一下${c.no}
这种格式的其实是我们后期为了做集合遍历的。这里先忽略掉。后面我们会着重介绍。到了这一步说明我们的前期准备就已经完成了。剩下我们就通过freemark就行方法调用导出就可以了。
首先我们构建freemark加载路径。就是设置一下freemark模板路径。模板路径中存放的就是我们上面编写好的模板。只不过这里的模板不是严格意义的word.而是通过word另存为xml格式的文件。
//创建配置实例
Configuration configuration = new Configuration();
//设置编码
configuration.setDefaultEncoding("UTF-8");
//ftl模板文件
configuration.setClassForTemplateLoading(OfficeUtils.class, "/template");
Template template = configuration.getTemplate(templateName);
Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
template.process(dataMap, out);
<#if checkbox ??&& checkbox?seq_contains(‘窒息;‘)?string(‘true‘,‘false‘)==‘true‘>0052<#else>00A3</#if>
<#list c as c>
dosomethings()
</#list>
#{}
格式编写。里面就是控制复选框的字段名。<w:sym w:font="Wingdings 2" w:char="0052"/>
<#if checkbox ??&& checkbox?seq_contains(‘窒息‘)?string(‘true‘,false‘)==‘true‘>0052<#else>00A3</#if>
Element root = document.getRootElement();
List<Element> checkList = root.selectNodes("//w:sym");
List<String> nameList = new ArrayList<>();
Integer indext = 1;
for (Element element : checkList) {
Attribute aChar = element.attribute("char");
String checkBoxName = selectCheckBoxNameBySymElement(element.getParent());
aChar.setData(chooicedCheckBox(checkBoxName));
}
${a_b}
。首先我们通过遍历word中所以文本通过正则验证是否符合集合规范。符合我们获取到当前的行然后在行标签前添加#list标签。 然后将\({a_b}修改成\){a.b} 至于为什么一开始不设置a.b格式的。我这里只想说是公司文化导致的。我建议搭建如果是自己实现这一套功能的话采用a.b格式最好。
Element root = document.getRootElement();
//需要获取所有标签内容,判断是否符合
List<Element> trList = root.selectNodes("//w:t");
//rowlist用来处理整行数据,因为符合标准的会有多列, 多列在同一行只需要处理一次。
List<Element> rowList = new ArrayList<>();
if (CollectionUtils.isEmpty(trList)) {
return;
}
for (Element element : trList) {
boolean matches = Pattern.matches(REGEX, element.getTextTrim());
if (!matches) {
continue;
}
//符合约定的集合格式的才会走到这里
//提取出tableId 和columnId
Pattern compile = Pattern.compile(REGEX);
Matcher matcher = compile.matcher(element.getTextTrim());
String tableName = "";
String colName = "";
while (matcher.find()) {
tableName = matcher.group(1);
colName = matcher.group(2);
}
//此时获取的是w:t中的内容,真正需要循环的是w:t所在的w:tr,这个时候我们需要获取到当前的w:tr
List<Element> ancestorTrList = element.selectNodes("ancestor::w:tr[1]");
/*List<Element> tableList = element.selectNodes("ancestor::w:tbl[1]");
System.out.println(tableList);*/
Element ancestorTr = null;
if (!ancestorTrList.isEmpty()) {
ancestorTr = ancestorTrList.get(0);
//获取表头信息
Element titleAncestorTr = DomUtils.getInstance().selectPreElement(ancestorTr);
if (!rowList.contains(ancestorTr)) {
rowList.add(ancestorTr);
List<Element> foreachList = ancestorTr.getParent().elements();
if (!foreachList.isEmpty()) {
Integer ino = 0;
Element foreach = null;
for (Element elemento : foreachList) {
if (ancestorTr.equals(elemento)) {
//此时ancestorTr就是需要遍历的行 , 因为我们需要将此标签扩容到循环标签汇中
foreach = DocumentHelper.createElement("#list");
foreach.addAttribute("name", tableName+" as "+tableName);
Element copy = ancestorTr.createCopy();
replaceLineWithPointForeach(copy);
mergeCellBaseOnTableNameMap(titleAncestorTr,copy,tableName);
foreach.add(copy);
break;
}
ino++;
}
if (foreach != null) {
foreachList.set(ino, foreach);
}
}
} else {
continue;
}
}
}
@{imgField}
进行占位。然后通过dom4j将图片的base64字节码用${imgField}占位。
//图片索引下表
Integer index = 1;
//获取根路径
Element root = document.getRootElement();
//获取图片标签
List<Element> imgTagList = root.selectNodes("//w:binData");
for (Element element : imgTagList) {
element.setText(String.format("${img%s}",index++));
//获取当前图片所在的wp标签
List<Element> wpList = element.selectNodes("ancestor::w:p");
if (CollectionUtils.isEmpty(wpList)) {
throw new DomException("未知异常");
}
Element imgWpElement = wpList.get(0);
while (imgWpElement != null) {
try {
imgWpElement = DomUtils.getInstance().selectNextElement(imgWpElement);
} catch (DomException de) {
break;
}
//获取对应图片字段
List<Element> imgFiledList = imgWpElement.selectNodes("w:r/w:t");
if (CollectionUtils.isEmpty(imgFiledList)) {
continue;
}
String imgFiled = getImgFiledTrimStr(imgFiledList);
Pattern compile = Pattern.compile(REGEX);
Matcher matcher = compile.matcher(imgFiled);
String imgFiledStr = "";
while (matcher.find()) {
imgFiledStr = matcher.group(1);
boolean remove = imgWpElement.getParent().elements().remove(imgWpElement);
System.out.println(remove);
}
if (StringUtils.isNotEmpty(imgFiledStr)) {
element.setText(String.format("${%s}",imgFiledStr));
break;
}
}
}
dom操作xml
dom生成xml
httpclient获取反应流
获取jar路径
itext实现套打
ftl常见语法
freemark官网
ftl判断非空
freemark自定义函数
freemark自定义函数java
freemark特殊字符转义
java实现word转xml各种格式
原文:https://www.cnblogs.com/zhangxinhua/p/12954773.html