poi解析word中的表格

导入依赖

<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poiartifactId>
    <version>5.0.0version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-ooxmlartifactId>
    <version>5.0.0version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-scratchpadartifactId>
    <version>5.0.0version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poi-ooxml-fullartifactId>
    <version>5.0.0version>
dependency>
<dependency>
    <groupId>org.apache.poigroupId>
    <artifactId>poiartifactId>
    <version>5.0.0version>
dependency>

编码 ReadDocUtil

package com.hx.demo;


import org.apache.poi.poifs.filesystem.FileMagic;
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 java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Huathy
 * @date 2023-05-23 18:35
 * @description
 */
public class ReadDocUtil {
    public static Map<String, String> readDocxTable(InputStream is) throws Exception {
        Map<String, String> result = new HashMap<>();
        if (FileMagic.valueOf(is) == FileMagic.OOXML) {
            XWPFDocument xwpf = new XWPFDocument(is);//得到word文档的信息
            Iterator<XWPFTable> it = xwpf.getTablesIterator();//得到word中的表格
            while (it.hasNext()) {
                XWPFTable table = it.next();
                List<XWPFTableRow> rows = table.getRows();
                //读取每一行数据
                for (XWPFTableRow row : rows) {
                    //读取每一列数据
                    List<XWPFTableCell> cells = row.getTableCells();
                    String tempKey = "";
                    for (int i = 0; i < cells.size(); i++) {
                        XWPFTableCell cell = cells.get(i);
                        if (i % 2 == 0) {
                            tempKey = cell.getText();
                        } else {
                            result.put(tempKey, cell.getText());
                        }
                    }
                }
            }
        }
        return result;
    }
}

测试

package com.hx.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Map;

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) throws Exception {
        SpringApplication.run(DemoApplication.class, args);
        InputStream is = new BufferedInputStream(new FileInputStream("G:\\测试申请表.docx")); //really a OOXML Word file
        Map<String, String> map = ReadDocUtil.readDocxTable(is);
        System.out.println("map = " + map);
    }

}

poi解析word中的表格_第1张图片

测试结果

map = {使用单位名称=测试单位名称2, 负责人=测试负责人, 申请单位名称=测试单位名称1, 备注=测试备注, 负责人电话=13343214321, 申请依据=测试依据, 申请人电话=13312341234, 申请人=测试申请人}

你可能感兴趣的:(Java学习随笔,word,java,开发语言)