Java使用POI讀取Word中的表格


我的博客 地址:https://www.wenhaofan.com/a/20190627135921

代碼

package live.autu.word;

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;

/**
 * Hello world!
 *
 */

public class App {
	public static void main(String[] args) {
		//doc文檔路徑
		String filePath = "C:\\Users\\autu\\Desktop\\test.doc";
		//test.print(filePath,"第一個表");
		
		System.out.println(App.read(filePath,"第一個表"));;
	}

	/**
	 * 讀取文檔中表格
	 * @param filePath doc路徑
	 * @param set 第幾個表格
	 */
	public static String read(String filePath,String tableName) {

		StringBuilder sb=new StringBuilder();
		
		
		try (FileInputStream in = new FileInputStream(filePath); // 載入文檔
				POIFSFileSystem pfs = new POIFSFileSystem(in);
				HWPFDocument hwpf = new HWPFDocument(pfs);) {
		 
			Range range = hwpf.getRange();// 獲得文檔的讀取範圍
			TableIterator it = new TableIterator(range);
			// 迭代文檔中的表格
	 
			while (it.hasNext()) {
				Table tb = (Table) it.next();
			 
				// 迭代行,默認從0開始,能夠依據須要設置i的值,改變起始行數,也可設置讀取到那行,只需修改循環的判斷條件便可
				outer: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();
							// 去除後面的特殊符號
							if (null != s && !"".equals(s)) {
								s = s.substring(0, s.length() - 1);
							}
							s=s.trim();
							if(tableName.trim().equals(s)||i!=0) {
								sb.append(s + "\t");
							} else {
								break outer;
							}	
						}
					}
					sb.append( "\n");
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return sb.toString();
	}

}

依賴

<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi</artifactId>
    <version>4.0.1</version>
</dependency>
<dependency>
    <groupId>org.apache.poi</groupId>
    <artifactId>poi-scratchpad</artifactId>
    <version>4.0.1</version>
</dependency>

效果圖

        test.doc
java

        http://qiniu.wenhaofan.com/520520_20190627135716.png

         控制檯打印
        http://qiniu.wenhaofan.com/520520_20190627135748.pngapache

相關文章
相關標籤/搜索