在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(); // 换行
        }
    }
}
相关推荐
天若有情67315 小时前
打破思维定式!C++参数设计新范式:让结构体替代传统参数列表
java·开发语言·c++
斯特凡今天也很帅15 小时前
python测试SFTP连通性
开发语言·python·ftp
sunywz15 小时前
【JVM】(4)JVM对象创建与内存分配机制深度剖析
开发语言·jvm·python
亲爱的非洲野猪15 小时前
从ReentrantLock到AQS:深入解析Java并发锁的实现哲学
java·开发语言
星火开发设计15 小时前
C++ set 全面解析与实战指南
开发语言·c++·学习·青少年编程·编程·set·知识
wheelmouse778815 小时前
如何设置VSCode打开文件Tab页签换行
java·python
yangminlei15 小时前
Spring Boot——日志介绍和配置
java·spring boot
廋到被风吹走16 小时前
【Spring】Spring Boot Starter设计:公司级监控SDK实战指南
java·spring boot·spring
码头整点薯条16 小时前
启动报错:Invalid value type for attribute ‘factoryBeanObjectType‘ 解决方案
java
沛沛老爹16 小时前
Web开发者进阶AI:Agent Skills-深度迭代处理架构——从递归函数到智能决策引擎
java·开发语言·人工智能·科技·架构·企业开发·发展趋势