6

テキストを既存の .doc ファイルに追加するコードがあり、Apache POI を使用して別の名前で保存します。

以下は私がこれまでに試したコードです

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import org.apache.poi.xwpf.model.XWPFHeaderFooterPolicy;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFFooter;
import org.apache.poi.xwpf.usermodel.XWPFTable;

public class FooterTableWriting {

    public static void main(String args[])
    {
        String path="D:\\vignesh\\AgileDocTemplate.doc";
        String attch="D:\\Attach.doc";
        String comment="good";
        String stat="ready";
        String coaddr="xyz";
        String cmail="abc@gmail.com";
        String sub="comp";
        String title="Globematics";
        String cat="General";
        setFooter(path, attch, comment, stat, coaddr, cmail, sub, title, cat);
    }
    private static  void setFooter(String docTemplatePath,String attachmentPath,String comments,String status,String coAddress,String coEmail,String subject,String title,String catagory)
    {
          try{

                    InputStream input = new FileInputStream(new File(docTemplatePath));
                    XWPFDocument document=new XWPFDocument(input);
                    XWPFHeaderFooterPolicy headerPolicy =new XWPFHeaderFooterPolicy(document);
                    XWPFFooter footer = headerPolicy.getDefaultFooter();
                    XWPFTable[] table = footer.getTables();

                    for (XWPFTable xwpfTable : table)
                       {
                           xwpfTable.getRow(1).getCell(0).setText(comments);
                           xwpfTable.getRow(1).getCell(1).setText(status);
                           xwpfTable.getRow(1).getCell(2).setText(coAddress);
                           xwpfTable.getRow(1).getCell(3).setText(coEmail);
                           xwpfTable.getRow(1).getCell(4).setText(subject);
                           xwpfTable.getRow(1).getCell(5).setText(title);
                           xwpfTable.getRow(1).getCell(6).setText(catagory);

                       }

                  File f=new File (attachmentPath.substring(0,attachmentPath.lastIndexOf('\\')));

                  if(!f.exists())
                      f.mkdirs();

                  FileOutputStream out = new FileOutputStream(new File(attachmentPath));
                  document.write(out);
                  out.close();

                  System.out.println("Attachment Created!");

         }
          catch(Exception e)
          {
              e.printStackTrace();
          }

    }

}

以下は私が得たものです

    org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:124)
    at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:200)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.<init>(XWPFDocument.java:74)
    at ext.gt.checkOut.FooterTableWriting.setFooter(FooterTableWriting.java:32)
    at ext.gt.checkOut.FooterTableWriting.main(FooterTableWriting.java:25)
Caused by: org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main
    at org.apache.xmlbeans.impl.store.Locale.verifyDocumentType(Locale.java:458)
    at org.apache.xmlbeans.impl.store.Locale.autoTypeDocument(Locale.java:363)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1279)
    at org.apache.xmlbeans.impl.store.Locale.parseToXmlObject(Locale.java:1263)
    at org.apache.xmlbeans.impl.schema.SchemaTypeLoaderBase.parse(SchemaTypeLoaderBase.java:345)
    at org.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument$Factory.parse(Unknown Source)
    at org.apache.poi.xwpf.usermodel.XWPFDocument.onDocumentRead(XWPFDocument.java:92)
    ... 4 more

これに対応するすべての jar ファイルを追加しましたが、まだ解決策が見つかりません。この apache poi は初めてなので、説明と例を教えてください。ありがとう

4

10 に答える 10

6

質問に対する私のコメントからコピー:

Apache POI ディストリビューションに含まれている poi-ooxml-schemas.jar が必要なようです。1 つの jar を追加するだけでは、フレームワークのすべてのクラスがあるわけではありません。


私のコメント(または他の人の回答)に基づいて問題を解決した後、この新しい例外があります

org.apache.xmlbeans.XmlException: error: The document is not a document@http://schemas.openxmlformats.org/wordprocessingml/2006/main: document element mismatch got themeManager@http://schemas.openxmlformats.org/drawingml/2006/main

Apache POI - HWPF - Java API to Handle Microsoft Word Files を読むと、間違ったクラスを使用して 2003- word 文書を処理しているように見えます: HWPF は、Microsoft Word 97(-2007) ファイル形式のポートの名前ですpure Java ...新しい Word 2007 .docx 形式の HWPF のパートナーは XWPF です。. HWPFDocumentこれは、ドキュメントを処理するか、ドキュメントを Word 2003- から Word 2007+ に変更するためのクラスが必要であることを意味します。

IMO Apache POI は Excel ファイルを処理するための優れたソリューションだと思いますが、Word ドキュメントを処理するには別のオプションを検討します。関連情報を取得するには、この質問を確認してください。

于 2013-02-27T08:04:32.590 に答える
5

これは の依存階層ですpoi-ooxml-3.9.jar

ここに画像の説明を入力

つまり、コンパイル時に使用されなくても、実行時にそれらのいずれかを使用できます。

プロジェクトのクラスパスにすべての jar があることを確認してください。

于 2013-02-27T07:54:26.350 に答える
0

このエラーの経験を報告すると思いました。私は突然それを手に入れ始めましたが、ワークスペースでは何も変更していませんでした。複数のシートを持つExcelファイルを読み込もうとしているときに発生することが判明しました(2番目のシートはピボットテーブルで、大量のデータでした。データのサイズが原因かどうかはわかりません(私はそう思うので、 1 つ以上のワークシートを含む Excel ファイルを読んだことがあります)その 2 番目のシートを削除すると、機能しました.クラスパスを変更する必要はありません.

于 2016-06-09T16:44:55.220 に答える
0

このクラスは、こちらからorg.openxmlformats.schemas.wordprocessingml.x2006.main.DocumentDocument.Factory ダウンロードできる jar ooxml-schemas-1.0.jar にあります。

于 2013-02-27T07:52:02.247 に答える
0

org.apache.poi.POIXMLException: org.apache.xmlbeans.XmlException: 要素 themeManager@ http://schemas.openxmlformats.org/drawingml/2006/mainは有効なワークブックではありません@ http://schemas.openxmlformats.org/スプレッドシートml/2006/メインドキュメントまたは有効な代替。

解決策:- .xls の代わりに .xlsx 形式を使用する

于 2016-11-10T05:04:35.983 に答える
0

XWPFDocument に対する適切な依存関係がないため、このエラーが発生しています。ooxml-schemas には xmlbeans が必要で、ooxml には poi と ooxml-schemas が必要です。

ここをチェックしてください:http://poi.apache.org/overview.html#components

于 2014-12-01T00:29:53.780 に答える