POI读取word里面的表格并处理数据

来源https://www.freesion.com/article/757264781/
我这里对原文做了一些修改,原文就一些代码,然后写了一下自己踩的坑;先看一下我要导入的模板吧。
在这里插入图片描述
这个表格里的数据是我改完之后的,本来是公司某个部门的图纸,在表格上面原本有图片也被我删除了,有图片并不影响我读取数据;这里一共有四个表格,然后我还删除了很多行,红色框里面是我要提取的数据。
第一步添加依赖
注意版本最好对应,我在这里踩了坑

<!--POI导入依赖-->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>4.0.1</version>
        </dependency>

然后直接上代码
注意要导入的是哪个包

package com.wz.demo.controller;

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 org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.Iterator;
import java.util.List;

/**
 * @author: wz
 * @date: 2022/10/26
 * Buddha Bless, No Bug !
 */
@RestController
@RequestMapping("/wz/WordImportController")
public class WordImportController {

    @GetMapping("/wordImport")
    public void wordImport(MultipartFile file) throws Exception {
        InputStream in = file.getInputStream();//载入文档
        // 处理docx格式 即office2007以后版本
        if (file.getOriginalFilename().toLowerCase().endsWith("docx")) {
            //word 2007 图片不会被读取, 表格中的数据会被放在字符串的最后
            XWPFDocument xwpf = new XWPFDocument(in);//得到word文档的信息
            Iterator<XWPFTable> it = xwpf.getTablesIterator();//得到word中的表格
            // 设置需要读取的表格  set是设置需要读取的第几个表格,total是文件中表格的总数
            int set = 2, total = 4;
            int num = set;
            // 过滤前面不需要的表格
            for (int i = 0; i < set - 1; i++) {
                it.hasNext();
                it.next();
            }
            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();
                    for (int j = 0; j < cells.size(); j++) {
                        XWPFTableCell cell = cells.get(j);
                        //输出当前的单元格的数据
                        System.out.print(cell.getText() + "\t");
                    }
                    System.out.println();
                }
                // 过滤多余的表格
                while (num < total) {
                    it.hasNext();
                    it.next();
                    num += 1;
                }
            }
        } else {
            // 处理doc格式 即office2003版本
            POIFSFileSystem pfs = new POIFSFileSystem(in);
            HWPFDocument hwpf = new HWPFDocument(pfs);
            Range range = hwpf.getRange();//得到文档的读取范围
            TableIterator it = new TableIterator(range);
            // 迭代文档中的表格
            // 如果有多个表格只读取需要的一个 set是设置需要读取的第几个表格,total是文件中表格的总数
            int set = 1, total = 4;
            int num = set;
            for (int i = 0; i < set - 1; i++) {
                it.hasNext();
                it.next();
            }
            while (it.hasNext()) {
                Table tb = it.next();
                System.out.println("这是第" + num + "个表的数据");
                //迭代行,默认从0开始,可以依据需要设置i的值,改变起始行数,也可设置读取到那行,只需修改循环的判断条件即可
                for (int i = 0; i < tb.numRows(); i++) {
                    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);
                            }
                            System.out.print(s + "\t");
                        }
                    }
                    System.out.println();
                }
            }
        }
    }
}

然后看我的结果吧,我这里是直接打印在控制台的,我还没有处理数据,我打算弄个实体类存下来,然后把不需要的字符再去掉,可能会有些繁琐。
在这里插入图片描述
大家可以注意一下,word文档里面表格的单元格如果为空的话,这里也是空的,并不是什么null啊啥的。我这里并不需要对图片处理,所以我就没写相关处理图片的代码。

还有大家如果公司电脑装了加密软件的话,导入也是会报错的,代码会识别不出来,我也是找人解密了word文档才弄成功!

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值