Sfoglia il codice sorgente

fix '完成第一版word连接功能'

tangs 3 anni fa
parent
commit
4f8da3a24f

BIN
document/docx/02文件连接_插入文档.docx


docx/2013-黑龙江哈市-数学-2-2702-y.docx → document/docx/2013-黑龙江哈市-数学-2-2702-y.docx


docx/2013-黑龙江哈市-数学-2-2702.docx → document/docx/2013-黑龙江哈市-数学-2-2702.docx


+ 59 - 41
src/main/java/com/wzkj/zhai/contact/Application.java

@@ -1,8 +1,9 @@
 package com.wzkj.zhai.contact;
 import com.spire.doc.Document;
 import com.spire.doc.FileFormat;
+import com.spire.doc.Section;
+import com.spire.doc.documents.Paragraph;
 import org.apache.commons.io.FilenameUtils;
-import org.apache.commons.cli.*;
 
 
 import java.io.File;
@@ -12,57 +13,74 @@ import java.nio.file.Paths;
 public class Application {
     public static void main(String[] args) {
         String dir = "";
-        if (args.length > 0) {
-            dir = args[0];
+        String origin = "";
+        if (args.length == 2) {
+            origin = args[0];
+            dir = args[1];
+        } else {
+            System.out.printf("参悟不足,origin(%s), dir(%s)\n", origin, dir);
+            return;
         }
-        getAllFiles(dir);
-    }
-
-    public static void parseParams(String[] args) {
-        Options options = new Options();
-
+        parseOrigin(origin, dir);
     }
 
-    public static void merge(String dir, String filename) {
-        String ext = FilenameUtils.getExtension(filename);
-        if (!ext.equals("docx") && !ext.equals("dox")) {
-            return;
-        }
-        String answerDoc = filename.replace( "." + ext, "-y."+ext);
-        String resultDoc = filename.replace( "." + ext, "-r."+ext);
+    public static void parseOrigin(String origin, String dir) {
+        Document document = new Document(origin);
+        Document newDoc = new Document();
+        String text = document.getText();
+        String[] lines = text.split("\n");
 
-        //获取第一个文档的路径
-        String filePath1 = Paths.get(dir, filename).toString();
-        //获取第二个文档的路径
-        String filePath2 = Paths.get(dir, answerDoc).toString();
-        String resultPath = Paths.get(dir, resultDoc).toString();
+        String source = "";
+        String target = "";
 
-        //加载第一个文档
-        Document document = new Document(filePath1);
+        int index = 0;
+        Section section = newDoc.addSection();
+        for (int i = 0; i < lines.length; i ++, index ++) {
+            String line = lines[i].trim();
+            if (line.equals("")) {
+                continue;
+            }
 
-        //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档
-        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);
+            Paragraph paragraph = section.addParagraph();
+            paragraph.appendText(line);
+            System.out.printf("index: %d, line: %s, source: %s, target: %s\n", index, line, source, target);
 
-        //保存文档
-        document.saveToFile(resultPath, FileFormat.Docx_2013);
-    }
-
-    public static void getAllFiles(String dir) {
-        File dirFile = new File(dir);
-        File[] files = dirFile.listFiles();
-        if (null == files) {
-            System.out.printf("获取目录(%s)为空", dir);
-            return;
-        }
-
-        for (File file : files) {
-            if (file.isDirectory()) {
+            if (index < 2) {
                 continue;
             }
-            if (file.getName().contains("-y") || file.getName().contains("-r")) {
+
+            if (2 == index) {
+                source = line;
                 continue;
             }
-            merge(dir, file.getName());
+            if (3 == index) {
+                target = line;
+                index = -1;
+            }
+            System.out.printf("source: %s, target: %s\n", source, target);
+
+            merge(newDoc, dir, source, target);
+            source = "";
+            target = "";
         }
+
+        String newName = origin.replace(".docx", "-r.docx") ;;
+        newDoc.saveToFile(newName, FileFormat.Docx_2013);
+    }
+
+    public static void merge(Document newDoc, String dir, String source, String target) {
+
+        source = source + ".docx";
+        target = target + ".docx";
+
+
+        //获取第一个文档的路径
+        String filePath1 = Paths.get(dir, source).toString();
+        //获取第二个文档的路径
+        String filePath2 = Paths.get(dir, target).toString();
+
+        //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档
+        newDoc.insertTextFromFile(filePath1, FileFormat.Docx_2013);
+        newDoc.insertTextFromFile(filePath2, FileFormat.Docx_2013);
     }
 }