在Java中判断Word文档中是否包含表格并读取表格内容,可以使用Apache POI库教程

笔者这里整理的jar包包含pdf读写的jar包,jar包如图1所示

图1

读取world文件代码如下

复制代码
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.usermodel.Range;
import org.apache.poi.hwpf.usermodel.Table;
import org.apache.poi.hwpf.usermodel.TableIterator;
import org.apache.poi.xwpf.usermodel.*;

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

public class Testread {

    public static void main(String[] args) {

        String path="D://111.doc";
        if(path.indexOf(".docx")>-1){
            readdocxFile(path);
        }else{

            readDocTables("D://111.doc");
        }

    }
    public static void readDocTables(String filePath) {
        try (FileInputStream fis = new FileInputStream(filePath)) {
            HWPFDocument document = new HWPFDocument(fis);
            Range range = document.getRange();

            TableIterator iterator = new TableIterator(range);
            int tableCount = 0;

            while (iterator.hasNext()) {
                tableCount++;
                Table table = iterator.next();
                System.out.println("=== 表格 " + tableCount + " ===");
                readOldTable(table);
            }

            if (tableCount == 0) {
                System.out.println("文档中未找到表格");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void readOldTable(Table table) {
        for (int i = 0; i < table.numRows(); i++) {
            org.apache.poi.hwpf.usermodel.TableRow row = table.getRow(i);
            for (int j = 0; j < row.numCells(); j++) {
                String cellText = row.getCell(j).getParagraph(0).text();
                System.out.print(cellText.trim() + "\t");
            }
            System.out.println();
        }
    }
   public static  void  readdocxFile(String path){

       try (FileInputStream fis = new FileInputStream(path)) {
           XWPFDocument document = new XWPFDocument(fis);

           // 方法1:判断文档是否包含表格
           List<XWPFTable> tables = document.getTables();
           if (tables.isEmpty()) {
               System.out.println("文档中未找到表格");
           } else {
               System.out.println("找到 " + tables.size() + " 个表格");

               // 读取所有表格内容
               readAllTables(document);
           }

       } catch (Exception e) {
           e.printStackTrace();
       }
   }
    // 读取所有表格
    public static void readAllTables(XWPFDocument document) {
        List<XWPFTable> tables = document.getTables();

        for (int i = 0; i < tables.size(); i++) {
            System.out.println("=== 表格 " + (i + 1) + " ===");
            readTable(tables.get(i));
        }
    }

    // 读取单个表格
    public static void readTable(XWPFTable table) {
        for (XWPFTableRow row : table.getRows()) {
            for (XWPFTableCell cell : row.getTableCells()) {
                // 读取单元格文本
                String cellText = cell.getText();
                System.out.print(cellText + "\t");
            }
            System.out.println(); // 换行
        }
    }
}
相关推荐
我也要当昏君2 小时前
5.3 【2012统考真题】
开发语言·智能路由器·php
Kuo-Teng2 小时前
LeetCode 73: Set Matrix Zeroes
java·算法·leetcode·职场和发展
初见无风2 小时前
3.4 Boost库intrusive_ptr智能指针的使用
开发语言·boost
王元_SmallA2 小时前
服务器公网IP、私网IP、弹性IP是什么?区别与应
java·后端
程序猿20232 小时前
Python每日一练---第六天:罗马数字转整数
开发语言·python·算法
葵续浅笑3 小时前
LeetCode - 杨辉三角 / 二叉树的最大深度
java·数据结构·算法·leetcode
装不满的克莱因瓶3 小时前
【Java架构师】各个微服务之间有哪些调用方式?
java·开发语言·微服务·架构·dubbo·restful·springcloud
杨筱毅3 小时前
【穿越Effective C++】条款13:以对象管理资源——RAII原则的基石
开发语言·c++·effective c++