poi 解析word 求大神

danzhifan1 2016-10-14 11:05:26
jar包:
poi-3.9-20121203.jar
poi-examples-3.9-20121203.jar
poi-excelant-3.9-20121203.jar
poi-ooxml-3.9-20121203.jar
poi-ooxml-schemas-3.9-20121203.jar
poi-scratchpad-3.9-20121203.jar


import java.io.FileInputStream;

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableCell;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.hwpf.usermodel.TableRow;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class AnalysisYuanWord{
public static void main(String[] args){
try{
FileInputStream in = new FileInputStream("C:\\Users\\danzf\\Desktop\\预案测试.doc");//载入文档
//POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(in);
Range range = hwpf.getRange();//得到文档的读取范围
TableIterator it = new TableIterator(range);
//迭代文档中的表格
while (it.hasNext()) {
Table tb = (Table) it.next();
//迭代行,默认从0开始
for (int i = 0; i < tb.numRows(); i++) {
TableRow tr = tb.getRow(i);
//迭代列,默认从0开始
for (int j = 0; j < tr.numCells(); j++) {
TableCell td = tr.getCell(j);//取得单元格
//取得单元格的内容
for(int k=0;k<td.numParagraphs();k++){
Paragraph para =td.getParagraph(k);
String s = para.text();
System.out.println(s);
} //end for
} //end for
} //end for
} //end while
}catch(Exception e){
e.printStackTrace();
}
}//end method

}

POIFSFileSystem pfs = new POIFSFileSystem(in);
HWPFDocument hwpf = new HWPFDocument(in); 这两行报错:
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 org.apache.poi.hwpf.HWPFDocumentCore.verifyAndBuildPOIFS(HWPFDocumentCore.java:106)
at org.apache.poi.hwpf.HWPFDocument.<init>(HWPFDocument.java:174)
at AnalysisYuanWord.main(AnalysisYuanWord.java:68)

doc和docx都会报这个错,以为是包的冲突问题,全部换成poi-3.9版本还是不行
...全文
374 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
computerclass 2016-10-20
  • 打赏
  • 举报
回复
poi 解析word,想实现什么功能?问题解决没?????
Goskalrie 2016-10-14
  • 打赏
  • 举报
回复
类用错了,或是说没有做兼容性判断,报错信息中有相关的说明的: You need to call a different part of POI to process this data (eg XSSF instead of HSSF) 使用下面的类进行加载就可以,以前的项目……电脑重装后就没了……
FileInputStream in = new FileInputStream("E:\\filetest\\world2html\\test.doc");//载入文档
      org.apache.poi.xwpf.usermodel.XWPFDocument xwpf = new XWPFDocument(in);   
      org.apache.poi.xwpf.extractor.XWPFWordExtractor extractor = new XWPFWordExtractor(xwpf);
poi在线文档:http://tool.oschina.net/apidocs/apidoc?api=apache-POI 官网给的使用 HWPF and XWPF处理Microsoft Word说明:http://poi.apache.org/document/index.html 也可以到官网上下载API文档
jiarus 2016-10-14
  • 打赏
  • 举报
回复
http://labs.renren.com/apache-mirror//poi/release/bin/)下载了3.7的zip文件(poi-bin-3.7-20101029.zip),解压后将poi相关的包和xml相关的包都放上去。

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧