java poi 合并word_java使用poi对多个word文档进行合成

1 packagecom.kwm.referencemark.test;2

3 importjava.io.File;4 importjava.io.FileInputStream;5 importjava.io.FileOutputStream;6 importjava.io.OutputStream;7 importjava.util.ArrayList;8 importjava.util.HashMap;9 importjava.util.List;10 importjava.util.Map;11

12 importorg.apache.poi.openxml4j.opc.OPCPackage;13 importorg.apache.poi.xwpf.usermodel.BreakType;14 importorg.apache.poi.xwpf.usermodel.Document;15 importorg.apache.poi.xwpf.usermodel.XWPFDocument;16 importorg.apache.poi.xwpf.usermodel.XWPFPictureData;17 importorg.apache.xmlbeans.XmlOptions;18 importorg.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody;19 /**

20 * 合成word文档工具类21 * @ClassName: CompoundWordUtil22 */

23 public classCompoundWordUtil {24 public static void main (String[] args) throwsException {25 File newFile = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/Paticulars.docx");26 List srcfile = new ArrayList<>();27

28 File file1 = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/test1.docx");29 File file2 = new File("C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/test2.docx");30 srcfile.add(file1);31 srcfile.add(file2);32

33 try{34 OutputStream dest = newFileOutputStream(newFile);35 ArrayList documentList = new ArrayList<>();36 XWPFDocument doc = null;37 for (int i = 0; i < srcfile.size(); i++) {38 FileInputStream in = newFileInputStream(srcfile.get(i).getPath());39 OPCPackage open =OPCPackage.open(in);40 XWPFDocument document = newXWPFDocument(open);41 documentList.add(document);42 }43 for (int i = 0; i < documentList.size(); i++) {44 doc = documentList.get(0);45 if(i == 0){//首页直接分页,不再插入首页文档内容46 documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);47 //appendBody(doc,documentList.get(i));

48 }else if(i == documentList.size()-1){//尾页不再分页,直接插入最后文档内容49 appendBody(doc,documentList.get(i));50 }else{51 documentList.get(i).createParagraph().createRun().addBreak(BreakType.PAGE);52 appendBody(doc,documentList.get(i));53 }54 }55 doc.write(dest);56 System.out.println("*****合成成功********");57 Runtime.getRuntime().exec("cmd /c start winword C:/Users/v-guoxiao/Documents/trademark/TMReferencemark/testfile/Paticulars.docx");//直接调用cmd打开合成文档58 } catch(Exception e) {59 e.printStackTrace();60 }61 }62

63 public static void appendBody(XWPFDocument src, XWPFDocument append) throwsException {64 CTBody src1Body =src.getDocument().getBody();65 CTBody src2Body =append.getDocument().getBody();66

67 List allPictures =append.getAllPictures();68 //记录图片合并前及合并后的ID

69 Map map = new HashMap();70 for(XWPFPictureData picture : allPictures) {71 String before =append.getRelationId(picture);72 //将原文档中的图片加入到目标文档中

73 String after =src.addPictureData(picture.getData(), Document.PICTURE_TYPE_PNG);74 map.put(before, after);75 }76 appendBody(src1Body, src2Body,map);77 }78

79 private static void appendBody(CTBody src, CTBody append,Map map) throwsException {80 XmlOptions optionsOuter = newXmlOptions();81 optionsOuter.setSaveOuter();82 String appendString =append.xmlText(optionsOuter);83

84 String srcString =src.xmlText();85 String prefix = srcString.substring(0,srcString.indexOf(">")+1);86 String mainPart = srcString.substring(srcString.indexOf(">")+1,srcString.lastIndexOf("") + 1, appendString.lastIndexOf("

91 for (Map.Entryset : map.entrySet()) {92 addPart =addPart.replace(set.getKey(), set.getValue());93 }94 }95 //将两个文档的xml内容进行拼接

96 CTBody makeBody = CTBody.Factory.parse(prefix+mainPart+addPart+sufix);97 src.set(makeBody);98 }99 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值