1

「キラー」などの特定の単語が含まれている場合、docx の特定の行を削除したいと考えています。poi xwpf を使用してプログラムを作成するにはどうすればよいですか? 空のデータに置き換えると、行はそのまま残ります。

実際には、docx ファイルで特定のテキストを見つけることができるので、以下のコードを使用してその特定の行を一致させて削除するかどうかを決定できます。

for (XWPFParagraph xwpfParagraph : xwpfParagraphs) {
        List<XWPFRun> xwpfRuns = xwpfParagraph.getRuns();
        for (XWPFRun xwpfRun : xwpfRuns) {
            String xwpfRunText = xwpfRun.getText(xwpfRun
                    .getTextPosition());
            System.out.println(xwpfRunText);
            for (Map.Entry<String, String> entry : replcementMap
                    .entrySet()) {
                if (xwpfRunText != null
                        && xwpfRunText.contains(entry.getKey())) {
                    xwpfRunText = xwpfRunText.replaceAll(
                            entry.getKey() , entry.getValue());
                }
            }
            xwpfRun.setText(xwpfRunText, 0);
        }
    }
4

1 に答える 1

0

私も同じ問題を抱えていました。doc ファイル内のテキストを置き換えたいと思っていました。ここに素晴らしい例があります: http://moodygeeky.wordpress.com/ 2 クラスをインポートするだけです:

There are 2 classes:

ZipUtility – ファイルの圧縮と解凍を処理する
SubstituteText – .docx ファイル内のテキストの置換用

そして、これらの文字列を好みに置き換えてください:

// Names of placeholders, starting and ending with % (to be updated
    // accordingly)
    String placeholder1 = "%name%";
    String placeholder2 = "%text%";

    // Values to replace placeholders (to be updated accordingly)
    String var1 = escapeHTML("newName"); // %name%
    String var2 = escapeHTML("newText"); // %text%
于 2014-11-13T12:56:53.547 に答える