简体   繁体   English

使用Java保存Word文件(可运行的JAR)

[英]Save Word-file with Java (runnable JAR)

I´m trying to build an application which can read information from Excel files and place the data into a document. 我正在尝试构建一个可以从Excel文件读取信息并将数据放入文档的应用程序。 The document is a form of a template with columns in. Everything works just fine until the saving part. 该文档是带有列的模板的一种形式。一切正常,直到保存部分为止。

It work, almost, like it should when I run the program directly in IntelliJ. 它几乎可以像我直接在IntelliJ中运行程序时那样工作。 However, when I install the application to an runnable JAR with Maven - the JAR won't work. 但是,当我使用Maven将应用程序安装到可运行的JAR时,JAR将无法工作。

The file is saved as desired... But the new file contains nothing if I run the application from JAR. 该文件已按需要保存...但是,如果我从JAR运行该应用程序,则新文件将不包含任何内容。 When i run directly in IntelliJ, the new file is been created and opened, but of 3 columns / row, only 2 has data in it. 当我直接在IntelliJ中运行时,将创建并打开新文件,但每行3列,只有2列有数据。

What can I do? 我能做什么?

Link to template document 链接到模板文件

http://www.labelmedia.de/englisch/doc/70%20x%2032%20mm%20-%20Art.%2088%2010%2027%2070%2032.doc http://www.labelmedia.de/englisch/doc/70%20x%2032%20mm%20-%20Art.%2088%2010%2027%2070%2032.doc

Thank you in advanced 谢谢高级

package utils;

import model.Customers;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;

public class WriteToDocument {
    String TARGET_FILE = "src\\main\\java\\utils\\template\\template.doc";
    private int postInList = 0;

    public WriteToDocument() {}

    public WriteToDocument(ArrayList<Customers> list) throws IOException {
        list.remove(0);

        HWPFDocument doc = null;
        try {
            doc = openDocument(TARGET_FILE);

            Range range = doc.getRange();
            TableIterator itr = new TableIterator(range);
            while (itr.hasNext()) {
                Table table = itr.next();
                for (int rowIndex = 0; rowIndex < table.numRows(); rowIndex++) {
                    TableRow row = table.getRow(rowIndex);
                    for (int colIndex = 0; colIndex < row.numCells(); colIndex++) {
                        TableCell cell = row.getCell(colIndex);

                        //WRITE IN TABLE //
                        if (postInList < list.size()) {
                            cell.getParagraph(0).replaceText(list.get(postInList).getName() + "\n\r" +
                                    "\n\r" + list.get(postInList).getAddress() + "\n\r" +
                                    list.get(postInList).getPostcode() + " " + list.get(postInList).getCity(), false);
                            postInList++;
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            saveDocument(doc);

        }
    }

    private HWPFDocument openDocument(String file) throws Exception {
        System.out.println("OPEN");
        return new HWPFDocument(new POIFSFileSystem(new FileInputStream(file)));
    }

    private static void saveDocument(HWPFDocument doc) throws IOException {
        System.out.println("SAVE");
        try (FileOutputStream out = new FileOutputStream(new File("test.doc"))) {
            doc.write(out);
            out.flush();
            System.out.println("File saved");
            doc.close();
            out.close();
            Desktop dt = Desktop.getDesktop();
            dt.open(new File("test.doc"));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
        }
    }

Stacktrace 堆栈跟踪

    java.io.FileNotFoundException: src\main\java\utils\template\template.doc (Det går inte att hitta sökvägen)
        at java.io.FileInputStream.open0(Native Method)
        at java.io.FileInputStream.open(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at java.io.FileInputStream.<init>(Unknown Source)
        at utils.WriteToDocument.openDocument(WriteToDocument.java:56)
        at utils.WriteToDocument.<init>(WriteToDocument.java:25)
        at utils.ReadExcel.writeToDocument(ReadExcel.java:64)
        at utils.ReadExcel.<init>(ReadExcel.java:57)
        at MainFrameController$1.actionPerformed(MainFrameController.java:31)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)
SAVE
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
        at utils.WriteToDocument.saveDocument(WriteToDocument.java:62)
        at utils.WriteToDocument.<init>(WriteToDocument.java:49)
        at utils.ReadExcel.writeToDocument(ReadExcel.java:64)
        at utils.ReadExcel.<init>(ReadExcel.java:57)
        at MainFrameController$1.actionPerformed(MainFrameController.java:31)
        at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
        at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
        at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
        at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
        at java.awt.Component.processMouseEvent(Unknown Source)
        at javax.swing.JComponent.processMouseEvent(Unknown Source)
        at java.awt.Component.processEvent(Unknown Source)
        at java.awt.Container.processEvent(Unknown Source)
        at java.awt.Component.dispatchEventImpl(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
        at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
        at java.awt.Container.dispatchEventImpl(Unknown Source)
        at java.awt.Window.dispatchEventImpl(Unknown Source)
        at java.awt.Component.dispatchEvent(Unknown Source)
        at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
        at java.awt.EventQueue.access$500(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.awt.EventQueue$3.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.awt.EventQueue$4.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
        at java.awt.EventQueue.dispatchEvent(Unknown Source)
        at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
        at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
        at java.awt.EventDispatchThread.run(Unknown Source)

Most probably you are missing libraries when it is exported as a JAR. 将其导出为JAR时,很可能缺少库。 You are using Apache POI as external libraries. 您正在使用Apache POI作为外部库。 Inside the IDE all the libraries were applied but when exported, they seems to be missing. 在IDE内部,所有库都已应用,但是在导出时似乎丢失了。 Just run the jar file from command prompt to be 100% clear about the issue. 只需在命令提示符下运行jar文件,即可100%清除此问题。 In IDEs like NetBeans the libraries are exported into a separate folder called lib have a look for similar related to your IDE as well. 在像NetBeans这样的IDE中,库被导出到名为lib的单独文件夹中,该文件夹也将查找与您的IDE相关的类似文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM