1、首先引入mvn
<dependency>
<groupId>io.github.java-diff-utils</groupId>
<artifactId>java-diff-utils</artifactId>
<version>4.11</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.0.0</version>
</dependency>
2、编写解析word为text函数
public List<String> parseWord(String input){
try{
InputStream inputStream = new FileInputStream(input);
XWPFDocument document = new XWPFDocument(inputStream);
List<String> pList = new ArrayList<>();
Map<String, Poi.PraInfo> pMap = new HashMap<>();
Integer lineNum = 1;
List<XWPFParagraph> paras = document.getParagraphs();
if (!CollectionUtils.isEmpty(paras)) {
for (XWPFParagraph para : paras) {
//当前段落的属性
//CTPPr pr = para.getCTP().getPPr();
String text = para.getText();
if (StringUtils.isBlank(text)) {
// continue;
}
pList.add(text);
}
}
return pList;
}catch (Exception e){
e.printStackTrace();
}
return new ArrayList<>();
}
3、编写差异函数
public void diff1(){
try {
String sourceFile = "D:/java_work_space/test/doc_comparer/2.docx";
String targetFile = "D:/java_work_space/test/doc_comparer/2-1.docx";
//原始文件
List<String> original = parseWord(sourceFile);
//对比文件
List<String> revised = parseWord(targetFile);
//两文件的不同点
Patch<String> patch = DiffUtils.diff(original, revised);
//生成统一的差异格式
List<String> unifiedDiff = UnifiedDiffUtils.generateUnifiedDiff("test1.txt", "test2.txt", original, patch, 0);
for(int i=2;i<unifiedDiff.size();i++){
System.out.println(unifiedDiff.get(i));
}
}catch (Exception e){
e.printStackTrace();
}
}
标签:java,String,DiffUtils,List,text,poi,new,文本
From: https://blog.51cto.com/u_15595167/8017115