0

私の最初のクラスを docx4j ( http://www.docx4java.org ) で書こうとしています。基本的には、.docx ファイル内のテキストの文字列を見つけて、それを別のテキストの文字列に置き換えるという考え方です。基本的に差し込み印刷です。エラーは発生していませんが、マージされたドキュメント自体は、提案したパスに保存されていません。これはファイルパスの問題だと思いますが、何も問題はありません。

package efi.mailmerge.servlets;

import java.util.List;
import javax.xml.bind.JAXBElement;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.Text;

public class WordDocTest {

    /**
     * Open word document /Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample.docx, replace a piece of text and save
     * the result to /Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample-Out.docx.
     *
     * The text <<CUS_FNAME>> will be replaced with John.
     *
     * @param args
     */
    public static void main(String[] args) {

        // Text nodes begin with w:t in the word document
        final String XPATH_TO_SELECT_TEXT_NODES = "//w:t";

        try {
            // Open the input file
            WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new java.io.File("/Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample.docx"));

            // Build a list of "text" elements
            List texts = wordMLPackage.getMainDocumentPart().getJAXBNodesViaXPath(XPATH_TO_SELECT_TEXT_NODES, true);

            // Loop through all "text" elements
            for (Object obj : texts) {
                Text text = (Text) ((JAXBElement) obj).getValue();

                // Get the text value
                String textValueBefore = text.getValue();

                // Perform the replacement
                String textValueAfter = textValueBefore.replaceAll("<<CUS_FNAME>>", "John");

                // Show the element before and after the replacement
                System.out.println("textValueBefore = " + textValueBefore);
                System.out.println("textValueAfter = " + textValueAfter);

                // Update the text element now that we have performed the replacement
                text.setValue(textValueAfter);

            }

            wordMLPackage.save(new java.io.File("/Users/Jeff/Development/ReServe-Unleashed/Dev/MailMerge/uploads/Sample-Out.docx"));

        } catch (Docx4JException e) {
            Logger.getLogger(WordDocTest.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        } catch (Exception e) {
            Logger.getLogger(WordDocTest.class.getName()).log(Level.SEVERE, null, e);
            e.printStackTrace();
        }
    }

}

26 行目と 50 行目で、入出力パスを確認できます。Sample.docx 入力ファイルが存在し、uploads ディレクトリに書き込み権限があることを確認しました。ここで私のファイルパスに何か問題がありますか? 私は完全に間違った道を進んでいる可能性がありますが、これはすべて私にとって非常に新しいことなので、私は進みながら学んでいます.

すべてのヘルプは非常に高く評価されています。

4

1 に答える 1

0

一見すると、次のように記述されたパスを試してみることをお勧めします。

wordMLPackage.save(new java.io.File("\\Users\\Jeff\\Development\\ReServe-Unleashed\\Dev\\MailMerge\\uploads\\Sample-Out.docx"));

それでも動作しない場合は、スタック トレースを提供してください。それは役立つかもしれません。(ドキュメントが保存されていない場合は、例外がスローされる必要があります)

于 2013-05-23T10:48:44.397 に答える