在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(); // 换行
        }
    }
}
相关推荐
算法与双吉汉堡17 小时前
【短链接项目笔记】Day2 用户注册
java·redis·笔记·后端·spring
2501_9307077817 小时前
使用C#代码更改 PowerPoint 幻灯片大小
开发语言·c#·powerpoint
CoderCodingNo17 小时前
【GESP】C++三级真题 luogu-B4414 [GESP202509 三级] 日历制作
开发语言·c++·算法
bug总结17 小时前
前端开发中为什么要使用 URL().origin 提取接口根地址
开发语言·前端·javascript·vue.js·html
北漂IT民工_程序员_ZG18 小时前
SpringBean生命周期,动态代理
java·spring boot·spring
晨曦夜月18 小时前
笔试强训day7
开发语言·c++·算法
Kurbaneli18 小时前
先啃C语言还是直奔目标?
开发语言
老华带你飞18 小时前
建筑材料管理|基于springboot 建筑材料管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·spring
木心爱编程18 小时前
【Qt 5.14.2 新手实战】QTC++入门筑基——按钮与标签联动:QPushButton + QLabel 实现图片切换器
java·c++·qt
weixin_3077791318 小时前
Jenkins Pipeline 完全指南:核心概念、使用详解与最佳实践
开发语言·ci/cd·自动化·jenkins·etl