Prechádzať zdrojové kódy

add 完成word合并程序

tangs 3 rokov pred
rodič
commit
e9906a98d0

+ 2 - 0
.gitignore

@@ -12,3 +12,5 @@
 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
 hs_err_pid*
 
+*.iml
+.idea

BIN
docx/2013-黑龙江哈市-数学-2-2702-r.docx


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


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


+ 57 - 0
pom.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>com.wzkj.zhai</groupId>
+    <artifactId>contact</artifactId>
+    <version>1.0-SNAPSHOT</version>
+
+    <properties>
+        <maven.compiler.source>1.8</maven.compiler.source>
+        <maven.compiler.target>1.8</maven.compiler.target>
+        <java.version>11</java.version>
+    </properties>
+
+    <repositories>
+        <repository>
+            <id>com.e-iceblue</id>
+            <name>e-iceblue</name>
+            <url>http://repo.e-iceblue.com/nexus/content/groups/public/</url>
+        </repository>
+    </repositories>
+
+    <dependencies>
+        <dependency>
+            <groupId>javax.xml.bind</groupId>
+            <artifactId>jaxb-api</artifactId>
+            <version>2.3.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId> e-iceblue </groupId>
+            <artifactId>spire.doc.free</artifactId>
+            <version>3.9.0</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.apache.commons</groupId>
+            <artifactId>commons-lang3</artifactId>
+            <version>3.3.2</version>
+        </dependency>
+        <dependency>
+            <groupId>commons-io</groupId>
+            <artifactId>commons-io</artifactId>
+            <version>2.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>commons-cli</groupId>
+            <artifactId>commons-cli</artifactId>
+            <version>1.0</version>
+        </dependency>
+
+    </dependencies>
+
+</project>

+ 62 - 0
src/main/java/com/wzkj/zhai/contact/Application.java

@@ -0,0 +1,62 @@
+package com.wzkj.zhai.contact;
+import com.spire.doc.Document;
+import com.spire.doc.FileFormat;
+import org.apache.commons.io.FilenameUtils;
+import org.apache.commons.cli.*;
+
+
+import java.io.File;
+import java.nio.file.Paths;
+
+
+public class Application {
+    public static void main(String[] args) {
+        System.out.println(args[0]);
+//        getAllFiles("docx");
+    }
+
+    public static void parseParams(String[] args) {
+        Options options = new Options();
+
+    }
+
+    public static void merge(String dir, String filename) {
+        String ext = FilenameUtils.getExtension(filename);
+        String answerDoc = filename.replace( "." + ext, "-y."+ext);
+        String resultDoc = filename.replace( "." + ext, "-r."+ext);
+
+        //获取第一个文档的路径
+        String filePath1 = Paths.get(dir, filename).toString();
+        //获取第二个文档的路径
+        String filePath2 = Paths.get(dir, answerDoc).toString();
+        String resultPath = Paths.get(dir, resultDoc).toString();
+
+        //加载第一个文档
+        Document document = new Document(filePath1);
+
+        //使用insertTextFromFile方法将第二个文档的内容插入到第一个文档
+        document.insertTextFromFile(filePath2, FileFormat.Docx_2013);
+
+        //保存文档
+        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()) {
+                continue;
+            }
+            if (file.getName().contains("-y") || file.getName().contains("-r")) {
+                continue;
+            }
+            merge(dir, file.getName());
+        }
+    }
+}