开发者

Reading MS Word 2007 using Java

I am trying to read a Microsoft word file through Java. I have included all the .jar files from Apache poi-3.8-beta1 to my classpath. However, when I try running this, I get the following exception:

org.apache.poi.poifs.filesystem.OfficeXmlFileException: The supplied data appears to be in the Office 2007+ XML. You are calling the part of POI that deals with OLE2 Office Documents. You need to call a different part of POI to process this data (eg XSSF instead of HSSF)
        at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:131)
        at org.apache.poi.poifs.storage.HeaderBlock.<init>(HeaderBlock.java:104)
        at org.apache.poi.poifs.filesystem.POIFSFileSystem.<init>(POIFSFileSystem.java:138)
        at readingmsword07.Main.main(Main.java:27)

Following is my code:

import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;


public class Main {


    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("C:\\TrialDoc.docx");
            POIFSFileSystem fileSystem = new POIFSFileSystem(fis);            
            org.apache.poi.xwpf.extractor.XWPFWordExtractor oleTextExtractor =
            new XWPFWordExtrac开发者_如何学Ctor(new XWPFDocument(fis));
            System.out.print(oleTextExtractor.getText());            
        } catch (Exception e) {
                e.printStackTrace();
        }
    }

}

I am using the XWPFWordExtractor since I am trying to read a 2007 word document but for some reason I am unable to figure out the right POI that deals with this.

Any help is much appreciated. Thanks in advance!

~ Woods


remove the line,

POIFSFileSystem fileSystem = new POIFSFileSystem(fis);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜