java word 2007,使用Java阅读MS Word 2007

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.(HeaderBlock.java:131)

at org.apache.poi.poifs.storage.HeaderBlock.(HeaderBlock.java:104)

at org.apache.poi.poifs.filesystem.POIFSFileSystem.(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 XWPFWordExtractor(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);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值