说明:此项目需要导入POI-3.8包,请注意版本必须是3.8的,低版本不行,高版本的没试过。可以解析word2003和2007。
jar包:包括
dom4j-1.6.1.jar
stax-api-1.0.1.jar
xmlbeans-2.3.0.jar,
poi-3.8-20120326.jar
poi-examples-3.8-20120326.jar
poi-excelant-3.8-20120326.jar
poi-ooxml-3.8-20120326.jar
poi-ooxml-schemas-3.8-20120326.jar
poi-scratchpad-3.8-20120326.jar
jar包下载网址:http://download.youkuaiyun.com/detail/wang524506/4849282
网上实例:http://blog.sina.com.cn/s/blog_77618e7b0101af31.html
本例是解析word中的内容和表格里的内容。
public Map readDoc() throws IOException {
String [] arrContent = null;
Map map = new HashMap();
List tableList = new ArrayList();
int a = 0;
XWPFDocument document = null;
try {
document = new XWPFDocument(POIXMLDocument.openPackage("E:/可及和连贯的患者服务(ACC).docx"));
} catch (Exception e) {
// TODO: handle exception可及和连贯的患者服务(ACC).docx
return null;
}
List<XWPFParagraph> list = document.getParagraphs();
int size = list.size();
arrContent = new String[size];
for(XWPFParagraph x:list){
arrContent[a] = x.getText();
a++;
}
map.put("arrContent", arrContent);
// System.out.println("paragraphList.size="+paragraphList.size());
// String text =document.getParagraphs().get(1).getText();
//获取表格部分的内容
Iterator<?> it = document.getTablesIterator();
while (it.hasNext()) {
XWPFTable table = (XWPFTable) it.next();
int rowcount = table.getNumberOfRows();
//第一行的列数
int rowCellsize = table.getRow(0).getTableCells().size();
int k=0;
for(int i=0;i<rowcount;i++){
XWPFTableRow row = table.getRow(i);
// System.out.println("第"+i+"hang");
if(table.getRow(i).getTableCells().size()==rowCellsize){
String [] arrCells = new String[rowCellsize];
for(int j=0;j<rowCellsize;j++){
String s= row.getCell(j).getText();
arrCells[j]=s;
}
tableList.add(k,arrCells);
map.put("tableList", tableList);
k++;
}
}
return map;
}
return null;
}
表格部分的内容样式如图: