Java 讀取Word中表格,支持doc、docx


Java 讀取Word中表格,支持doc、docx

1、在pom.xml文件,添加相關依賴支持。

     <!--Word操作-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.0</version>
        </dependency>

 

2、建立工具類

import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.*;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;

import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * 讀取word文檔中表格數據,支持doc、docx
*/ public class WordReaderUtil { public static void main(String[] args) { String url = "C:\\Users\\Desktop\\word\\120501.docx"; String url1 = "C:\\Users\\Desktop\\word\\120502.doc"; List<String> list = tableInWord(url1, 1); System.out.println(list); } /** * 讀取文檔中表格 * @param filePath 文檔路徑 * @param orderNum 設置需要讀取的第幾個表格 */ public static List<String> tableInWord(String filePath,Integer orderNum){ try{ FileInputStream in = new FileInputStream(filePath);//載入文檔 // 處理docx格式 即office2007以后版本 if(filePath.toLowerCase().endsWith("docx")){ //word 2007 圖片不會被讀取, 表格中的數據會被放在字符串的最后 XWPFDocument xwpf = new XWPFDocument(in);//得到word文檔的信息 Iterator<XWPFTable> itpre = xwpf.getTablesIterator();//得到word中的表格 int total = 0; while (itpre.hasNext()) { itpre.next(); total += 1; } Iterator<XWPFTable> it = xwpf.getTablesIterator();//得到word中的表格 // 設置需要讀取的表格 set是設置需要讀取的第幾個表格,total是文件中表格的總數 int set = orderNum; int num = set; // 過濾前面不需要的表格 for (int i = 0; i < set-1; i++) { it.hasNext(); it.next(); } List<String> tableList = new ArrayList<>(); while(it.hasNext()){ XWPFTable table = it.next(); System.out.println("這是第" + num + "個表的數據"); List<XWPFTableRow> rows = table.getRows(); //讀取每一行數據 for (int i = 0; i < rows.size(); i++) { XWPFTableRow row = rows.get(i); //讀取每一列數據 List<XWPFTableCell> cells = row.getTableCells(); List<String> rowList = new ArrayList<>(); for (int j = 0; j < cells.size(); j++) { XWPFTableCell cell = cells.get(j); rowList.add(cell.getText()); //輸出當前的單元格的數據 System.out.print(cell.getText()+"["+i+","+j+"]" + "\t"); } tableList.addAll(rowList); System.out.println(); } // 過濾多余的表格 while (num < total) { it.hasNext(); it.next(); num += 1; } } return tableList; }else{ // 處理doc格式 即office2003版本 POIFSFileSystem pfs = new POIFSFileSystem(in); HWPFDocument hwpf = new HWPFDocument(pfs); Range range = hwpf.getRange();//得到文檔的讀取范圍 TableIterator itpre = new TableIterator(range);;//得到word中的表格 int total = 0; while (itpre.hasNext()) { itpre.next(); total += 1; } TableIterator it = new TableIterator(range); // 迭代文檔中的表格 // 如果有多個表格只讀取需要的一個 set是設置需要讀取的第幾個表格,total是文件中表格的總數 int set = orderNum; int num = set; for (int i = 0; i < set-1; i++) { it.hasNext(); it.next(); } List<String> tableList = new ArrayList<>(); while (it.hasNext()) { Table tb = (Table) it.next(); System.out.println("這是第" + num + "個表的數據"); //迭代行,默認從0開始,可以依據需要設置i的值,改變起始行數,也可設置讀取到那行,只需修改循環的判斷條件即可 for (int i = 0; i < tb.numRows(); i++) { List<String> rowList = new ArrayList<>(); 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); } rowList.add(s); System.out.print(s+"["+i+","+j+"]" + "\t"); } } tableList.addAll(rowList); System.out.println(); } // 過濾多余的表格 while (num < total) { it.hasNext(); it.next(); num += 1; } } return tableList; } }catch(Exception e){ e.printStackTrace(); } return null; } }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM