| | |
| | | package com.product.file.service; |
| | | |
| | | import org.apache.poi.openxml4j.opc.OPCPackage; |
| | | import org.apache.poi.xwpf.usermodel.XWPFDocument; |
| | | import org.apache.tomcat.util.http.fileupload.IOUtils; |
| | | import cn.hutool.core.io.FileUtil; |
| | | import com.product.file.util.MergeWordDocuments; |
| | | import org.apache.xmlbeans.XmlOptions; |
| | | import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBody; |
| | | |
| | | import java.io.*; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | */ |
| | | public class MergeDoc { |
| | | |
| | | /** |
| | | * 合并docx文件 |
| | | * |
| | | * @param srcDocxs 需要合并的目标docx文件 |
| | | * @param response 合并后的docx输出文件 |
| | | */ |
| | | public static File mergeDoc(List<File> srcDocxs, String tempFilePath) { |
| | | OutputStream dest = null; |
| | | List<OPCPackage> opcpList = new ArrayList<>(); |
| | | int length = null == srcDocxs ? 0 : srcDocxs.size(); |
| | | /** |
| | | * 循环获取每个docx文件的OPCPackage对象 |
| | | */ |
| | | for (int i = 0; i < length; i++) { |
| | | File doc = srcDocxs.get(i); |
| | | OPCPackage srcPackage = null; |
| | | try { |
| | | srcPackage = OPCPackage.open(doc); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } |
| | | if (null != srcPackage) { |
| | | opcpList.add(srcPackage); |
| | | } |
| | | } |
| | | int opcpSize = opcpList.size(); |
| | | //获取的OPCPackage对象大于0时,执行合并操作 |
| | | if (opcpSize > 0) { |
| | | try { |
| | | dest = new FileOutputStream(tempFilePath); |
| | | XWPFDocument src1Document = new XWPFDocument(opcpList.get(0)); |
| | | CTBody src1Body = src1Document.getDocument().getBody(); |
| | | //OPCPackage大于1的部分执行合并操作 |
| | | if (opcpSize > 1) { |
| | | for (int i = 1; i < opcpSize; i++) { |
| | | OPCPackage src2Package = opcpList.get(i); |
| | | XWPFDocument src2Document = new XWPFDocument(src2Package); |
| | | CTBody src2Body = src2Document.getDocument().getBody(); |
| | | appendBody(src1Body, src2Body); |
| | | } |
| | | } |
| | | //将合并的文档写入目标文件中 |
| | | src1Document.write(dest); |
| | | return new File(tempFilePath); |
| | | } catch (FileNotFoundException e) { |
| | | e.printStackTrace(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | //关闭流 |
| | | IOUtils.closeQuietly(dest); |
| | | } |
| | | } |
| | | return null; |
| | | } |
| | | /** |
| | | * 合并docx文件 |
| | | * |
| | | * @param srcDocxs 需要合并的目标docx文件 |
| | | * @param response 合并后的docx输出文件 |
| | | */ |
| | | public static File mergeDoc(List<File> srcDocxs, String tempFilePath) throws Exception { |
| | | File file = null; |
| | | if (srcDocxs.size() > 1) { |
| | | file = srcDocxs.get(0); |
| | | for (int i = 1; i < srcDocxs.size(); i++) { |
| | | file = MergeWordDocuments.mergeFile(file, srcDocxs.get(i)); |
| | | } |
| | | File file1 = new File(tempFilePath); |
| | | FileUtil.copyFile(file, file1); |
| | | FileUtil.del(file); |
| | | return file1; |
| | | } |
| | | return file; |
| | | } |
| | | |
| | | /** |
| | | * 合并文档内容 |
| | | * |
| | | * @param src 目标文档 |
| | | * @param append 要合并的文档 |
| | | * @throws Exception |
| | | */ |
| | | private static void appendBody(CTBody src, CTBody append) throws Exception { |
| | | XmlOptions optionsOuter = new XmlOptions(); |
| | | optionsOuter.setSaveOuter(); |
| | | String appendString = append.xmlText(optionsOuter); |
| | | //替换word中得空行、根据自己需求来选择需不需要 |
| | | //word文档其实是由xml文件合成得,一下替换得是作者对应得xml文件中得空行标签 |
| | | /** |
| | | * 合并文档内容 |
| | | * |
| | | * @param src 目标文档 |
| | | * @param append 要合并的文档 |
| | | * @throws Exception |
| | | */ |
| | | private static void appendBody(CTBody src, CTBody append) throws Exception { |
| | | XmlOptions optionsOuter = new XmlOptions(); |
| | | optionsOuter.setSaveOuter(); |
| | | String appendString = append.xmlText(optionsOuter); |
| | | //替换word中得空行、根据自己需求来选择需不需要 |
| | | //word文档其实是由xml文件合成得,一下替换得是作者对应得xml文件中得空行标签 |
| | | // appendString = appendString.replaceAll("<w:p><w:pPr><w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"24\\\"/></w:rPr></w:pPr></w:p>",""); |
| | | // appendString = appendString.replaceAll("<w:p><w:pPr xsi:nil=\\\"true\\\" xmlns:xsi=\\\"xsi\\\"/></w:p>",""); |
| | | // appendString = appendString.replaceAll("<w:p><w:pPr><w:ind w:firstLineChars=\\\"200\\\" w:firstLine=\\\"600\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"30\\\"/><w:szCs w:val=\\\"30\\\"/></w:rPr></w:pPr></w:p>",""); |
| | | // appendString = appendString.replaceAll("<w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/>","");; |
| | | String srcString = src.xmlText(); |
| | | String srcString = src.xmlText(); |
| | | // srcString = srcString.replaceAll("<w:p><w:pPr><w:ind w:firstLineChars=\\\"200\\\" w:firstLine=\\\"600\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"30\\\"/><w:szCs w:val=\\\"30\\\"/></w:rPr></w:pPr></w:p>",""); |
| | | // srcString = srcString.replaceAll("<w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/>",""); |
| | | // srcString = srcString.replaceAll("<w:p><w:pPr><w:spacing w:line=\\\"360\\\" w:lineRule=\\\"auto\\\"/><w:jc w:val=\\\"right\\\"/><w:rPr><w:rFonts w:ascii=\\\"仿宋\\\" w:eastAsia=\\\"仿宋\\\" w:hAnsi=\\\"仿宋\\\"/><w:sz w:val=\\\"24\\\"/></w:rPr></w:pPr></w:p>",""); |
| | | // srcString = srcString.replaceAll("<w:p><w:pPr xsi:nil=\\\"true\\\" xmlns:xsi=\\\"xsi\\\"/></w:p>",""); |
| | | String prefix = srcString.substring(0, srcString.indexOf(">") + 1); |
| | | String mainPart = srcString.substring(srcString.indexOf(">") + 1, |
| | | srcString.lastIndexOf("<")); |
| | | String sufix = srcString.substring(srcString.lastIndexOf("<")); |
| | | String addPart = appendString.substring(appendString.indexOf(">") + 1, |
| | | appendString.lastIndexOf("<")); |
| | | CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart |
| | | + sufix); |
| | | src.set(makeBody); |
| | | } |
| | | String prefix = srcString.substring(0, srcString.indexOf(">") + 1); |
| | | String mainPart = srcString.substring(srcString.indexOf(">") + 1, |
| | | srcString.lastIndexOf("<")); |
| | | String sufix = srcString.substring(srcString.lastIndexOf("<")); |
| | | String addPart = appendString.substring(appendString.indexOf(">") + 1, |
| | | appendString.lastIndexOf("<")); |
| | | // CTBody makeBody = CTBody.Factory.parse(prefix + mainPart + addPart |
| | | // + sufix); |
| | | // src.set(makeBody); |
| | | } |
| | | |
| | | } |
| | | |