工作上需要用到POI来处理Word文档,于是这几天都在熟悉POI的API
一、Word文档格式
二、代码示例
/**
* 读取指定word文档
* @param file
* @return
*/
private static void readWordFile(File file) throws IOException {
XWPFDocument document = new XWPFDocument(new FileInputStream(file));
List<IBodyElement> bodyElements = document.getBodyElements();
for(IBodyElement bodyElement:bodyElements){
if(bodyElement instanceof XWPFParagraph && ((XWPFParagraph) bodyElement).getText().length() != 0){
System.out.println("是段落;"+((XWPFParagraph) bodyElement).getText());
System.out.println(((XWPFParagraph) bodyElement).getAlignment()+"....."+bodyElement.getPart()+"...."+((XWPFParagraph) bodyElement).getStyle());
}else if(bodyElement instanceof XWPFTable){
System.out.println("\n是表格:");
List<XWPFTableRow> rows = ((XWPFTable) bodyElement).getRows();
for(XWPFTableRow row : rows){
List<XWPFTableCell> cells = row.getTableCells();
for(XWPFTableCell cell:cells){
System.out.println(cell.getText());
}
}
}
}
}
结果展示
是段落;就是电风扇电风扇
LEFT.....Name: /word/document.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml....null
是段落;上的豆腐干豆腐干
CENTER.....Name: /word/document.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml....1
是段落;士大夫胜多负少
RIGHT.....Name: /word/document.xml - Content Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml....null
是表格:
序号
姓名
年龄
工资
01
王二狗
23
3733
Process finished with exit code 0