本文主要功能是解析word模板
这是一个word解析类,因为我做的系统用到了而且没有可用的帮助类,只能自己写。之前的实现方式是freemarker 模板解析。但是这次要求用poi不在使用freemarker。实现功能比较少,主要是满足开发需求即可,没有实现其它功能。实现功能如下:
1、word内文本内容解析
2、word内表格内容解析
3、word内图片内容解析
4、word脚注内容解析
功能实现的比较匆忙没有好好设计,如果可以将图标,图片,脚注等设置为实体类,便于配置管理。
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.util.Base64;
import java.util.List;
import java.util.Properties;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFootnote;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFPicture;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
import org.openxmlformats.schemas.drawingml.x2006.main.STSchemeColorVal;
import org.springframework.util.PropertyPlaceholderHelper;
import com.alibaba.cloud.commons.lang.StringUtils;
/**
* 通过word模板生成新的word工具类
**
*/
public class WordUtil {
public static final PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper("${", "}");
/**
* 根据模板生成新word文档 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
*
* @param textMap 需要替换的信息集合
* @return 成功返回true,失败返回false
*/
public static void changWord(InputStream inputStream, Properties properties, int height, int width) {
// InputStream in = null;
try {
// 获取docx解析对象
XWPFDocument document = new XWPFDocument(inputStream);
// 解析替换文本段落对象
WordUtil.changeText(document, properties);
// 解析替换表格对象
WordUtil.changeTable(document, properties);
// 替换文本中的图片
WordUtil.changePicture(document, properties, height, width);
// 脚注/尾注解析 footnote
WordUtil.changeFootNote(document, properties);
File file = new File("I://实体文件.docx");
FileOutputStream stream = new FileOutputStream(file);
document.write(stream);
stream.close();
document.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 尾注解析
*
* @param document
* @param properties
*/
public static void changeFootNote(XWPFDocument document, Properties properties) {
List<XWPFFootnote> footNoteList = document.getFootnotes();
for (XWPFFootnote footnote : f

&spm=1001.2101.3001.5002&articleId=135838883&d=1&t=3&u=ca8754b481204b99b6e67358a59341c6)
5617

被折叠的 条评论
为什么被折叠?



