src/main/java/com/product/file/controller/WebOfficeController.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/service/MergeDoc.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/util/FileUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/product/file/util/MergeWordDocuments.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/product/file/controller/WebOfficeController.java
@@ -176,11 +176,15 @@ } //æä»¶åï¼çªå£çååï¼ String file_title = fse.getString("fileName"); if (file_title == null) { file_title = ""; } else { file_title = UnicodeUtil.toString(file_title); FieldSetEntity fieldSetEntity = fileManagerService.getBaseDao().getFieldSetEntity("product_sys_attachments", fse.getString("uuid"), false); if (!FieldSetEntity.isEmpty(fieldSetEntity)) { file_title = fieldSetEntity.getString("file_name"); } // if (file_title == null) { // file_title = ""; // } else { // file_title = UnicodeUtil.toString(file_title); // } SystemUser currentUser = SpringMVCContextHolder.getCurrentUser(); // Boolean allowNestRed = false; src/main/java/com/product/file/service/MergeDoc.java
@@ -1,6 +1,7 @@ package com.product.file.service; 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; @@ -23,7 +24,7 @@ if (srcDocxs.size() > 1) { file = srcDocxs.get(0); for (int i = 1; i < srcDocxs.size(); i++) { file = com.product.file.util.FileUtil.mergeFile(file, srcDocxs.get(i)); file = MergeWordDocuments.mergeFile(file, srcDocxs.get(i)); } File file1 = new File(tempFilePath); FileUtil.copyFile(file, file1); src/main/java/com/product/file/util/FileUtil.java
@@ -3,8 +3,6 @@ import cn.hutool.core.lang.UUID; import com.aspose.words.Document; import com.aspose.words.SaveFormat; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.xwpf.NiceXWPFDocument; import com.google.common.collect.Maps; import com.product.common.lang.StringUtils; import com.product.core.config.Global; @@ -16,7 +14,6 @@ import java.io.*; import java.util.List; import java.util.Map; import java.util.Properties; import java.util.zip.ZipEntry; import java.util.zip.ZipOutputStream; @@ -31,6 +28,10 @@ public class FileUtil { private FileUtil() { } public static void main(String[] args) { System.out.println(FileUtil.checkDocType(new File("D:\\downloads\\å ³äºåè°è§£å³è¥¿å®ä¸è¾¾å°äº§ç½®ä¸æéå ¬å¸ååæ¿é¢å®èµéç管账æ·å»ç»ç¸å ³é®é¢ (1).doc"))); } public static String checkDocType(File file) { @@ -78,38 +79,7 @@ * @return */ public static File mergeFile(File source, File target) throws Exception { if ("doc".equals(FileUtil.checkDocType(source))) { source = toDocx(source); } boolean delTarget = false; if ("doc".equals(FileUtil.checkDocType(target))) { target = toDocx(target); delTarget = true; } XWPFTemplate template = XWPFTemplate.compile(source); NiceXWPFDocument xwpfDocument = template.getXWPFDocument(); try { NiceXWPFDocument sub = new NiceXWPFDocument(new FileInputStream((target))); NiceXWPFDocument newDoc = xwpfDocument.merge(sub); String tempFile = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + UUID.randomUUID() + ".docx"; try (FileOutputStream out = new FileOutputStream(tempFile)) { newDoc.write(out); newDoc.close(); return new File(tempFile); } catch (IOException e) { throw e; } finally { if (delTarget) { target.delete(); } newDoc.close(); sub.close(); } } catch (IOException e) { throw e; } catch (Exception e) { throw e; } return MergeWordDocuments.mergeFile(source, target); } public static File toDocx(File file) { src/main/java/com/product/file/util/MergeWordDocuments.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,52 @@ package com.product.file.util; import cn.hutool.core.lang.UUID; import com.product.core.config.Global; import com.spire.doc.*; import java.io.*; public class MergeWordDocuments { public static File mergeFile(File source, File target) throws Exception { if ("doc".equals(FileUtil.checkDocType(source))) { source = FileUtil.toDocx(source); } boolean delTarget = false; if ("doc".equals(FileUtil.checkDocType(target))) { target = FileUtil.toDocx(target); delTarget = true; } try (InputStream sourceIn = new FileInputStream(source); InputStream targetIn = new FileInputStream(target)) { Document sourceDoc = new Document(sourceIn); Document targetDoc = new Document(targetIn); for (Object section : targetDoc.getSections()) { Section sec = (Section) section; for (Object docObj : sec.getBody().getChildObjects()) { DocumentObject obj = (DocumentObject) docObj; Section lastSection = sourceDoc.getLastSection(); Body body = lastSection.getBody(); body.getChildObjects().add(obj.deepClone()); } } // doc.insertTextFromStream(targetIn, com.spire.doc.FileFormat.Docx); String tempFile = Global.getSystemConfig("temp.dir", "./attachment/temp") + File.separator + UUID.randomUUID() + ".docx"; try (FileOutputStream out = new FileOutputStream(tempFile)) { sourceDoc.saveToStream(out, FileFormat.Docx); return new File(tempFile); } catch (IOException e) { throw e; } finally { if (delTarget) { target.delete(); } // doc.close(); } } catch (IOException e) { throw e; } catch (Exception e) { throw e; } } }