poi按顺序读取office文档中的内容

package com.jeeplus.common.utils.word;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;

import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;



public class WordDataUtils {
	/**
     * word数据化存储
     * @param str
     * @return
	 * @throws IOException 
     */
    public static String toString(String path) throws IOException{
    	FileInputStream is = new FileInputStream(path);  
        XWPFDocument doc = new XWPFDocument(is);
        int index=0;
        String m="";
        int n=1;
        int csize=0;
        List<IBodyElement> list=doc.getBodyElements();
        for(IBodyElement iBodyElement:list){
        	if("PARAGRAPH".equals(iBodyElement.getElementType().toString())){//读取文档中段落
        		XWPFParagraph para=(XWPFParagraph)iBodyElement;
        		System.out.println(para.getText());
        	}else if("TABLE".equals(iBodyElement.getElementType().toString())){ //读取文档中表格
        		XWPFTable table=(XWPFTable)iBodyElement;
        		List<XWPFTableRow> rows = table.getRows();
        	       for(int y=0;y<rows.size();y++) {
        	        	  XWPFTableRow row=rows.get(y);
        	        	  List<XWPFTableCell> cells = row.getTableCells();
        	              csize=cells.size();
        	              for (int z=0;z<csize;z++) {  
        	            	  XWPFTableCell cell=cells.get(z);
        	            	  if(cell.getCTTc().getTcPr().getGridSpan()!=null){
        	            	  n=Integer.parseInt(cell.getCTTc().getTcPr().getGridSpan().getVal().toString());
        	            	  }
        	            	  m="";
        	            	  if(cell.getCTTc().getTcPr().getVMerge()!=null){
        	            	  if(cell.getCTTc().getTcPr().getVMerge().getVal()!=null){
        	            	  m=cell.getCTTc().getTcPr().getVMerge().getVal().toString();
        	            	  }else{
        	                  m=null;
        	                  }
        	            	  }
        	                  System.out.print(index+"-"+csize+"-"+y+"-"+z+"-"+n+"-"+m+":"+cell.getText()+"     ");  
        	              }
        	              System.out.println();
        	           } 
        	}
        }
        is.close();
        return "";
    }
    
    public static void main( String[] args ) throws IOException{
    	toString("C:\\Users\\admin\\Desktop\\553e34d7-e899-4f5c-a1e7-1889f5c2dfd0.docx");
	}
}

可以关注以下公众号交流:
在这里插入图片描述

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值