在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(); // 换行
        }
    }
}
相关推荐
躺平大鹅20 分钟前
Java面向对象入门(类与对象,新手秒懂)
java
初次攀爬者1 小时前
RocketMQ在Spring Boot上的基础使用
java·spring boot·rocketmq
花花无缺1 小时前
搞懂@Autowired 与@Resuorce
java·spring boot·后端
Derek_Smart3 小时前
从一次 OOM 事故说起:打造生产级的 JVM 健康检查组件
java·jvm·spring boot
NE_STOP4 小时前
MyBatis-mybatis入门与增删改查
java
孟陬7 小时前
国外技术周刊 #1:Paul Graham 重新分享最受欢迎的文章《创作者的品味》、本周被划线最多 YouTube《如何在 19 分钟内学会 AI》、为何我不
java·前端·后端
想用offer打牌7 小时前
一站式了解四种限流算法
java·后端·go
华仔啊7 小时前
Java 开发千万别给布尔变量加 is 前缀!很容易背锅
java
也些宝8 小时前
Java单例模式:饿汉、懒汉、DCL三种实现及最佳实践
java