ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

JAVA使用hutool poi工具读取Excel表格指定行列范围的数据

2022-01-07 13:04:03  阅读:247  来源: 互联网

标签:JAVA rowIndex Excel hutool value cell cellIndex int public


1.pom.xml依赖配置

    <dependencies>
        <!-- huTool工具箱 -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.9</version>
        </dependency>

        <!-- poi -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
    </dependencies>

 

2.代码

package com.hdwang.exceltest;

import cn.hutool.json.JSONUtil;

/**
 * 单元格数据
 */
public class CellData {

    /**
     * 行号
     */
    private int rowIndex;

    /**
     * 列号
     */
    private int cellIndex;

    /**
     * 单元格数值
     */
    private Object value;

    public int getRowIndex() {
        return rowIndex;
    }

    public void setRowIndex(int rowIndex) {
        this.rowIndex = rowIndex;
    }

    public int getCellIndex() {
        return cellIndex;
    }

    public void setCellIndex(int cellIndex) {
        this.cellIndex = cellIndex;
    }

    public Object getValue() {
        return value;
    }

    public void setValue(Object value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return JSONUtil.toJsonStr(this);
    }
}
package com.hdwang.exceltest;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import cn.hutool.poi.excel.cell.CellHandler;
import org.apache.poi.ss.usermodel.*;

import java.io.File;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

public class Main {
    public static void main(String[] args) {
        File templateFile = new File("C:\\Users\\hdwang\\Desktop\\test.xlsx");
        List<List<CellData>> rowDataList = readExcelData(templateFile, 2, Integer.MAX_VALUE, 1, Integer.MAX_VALUE);
        System.out.println(rowDataList);
    }

  

    /**
     * 读取表格数据
     *
     * @param templateFile   文件
     * @param startRowIndex  起始行号(从0开始)
     * @param endRowIndex    结束行号(从0开始)
     * @param startCellIndex 起始列号(从0开始)
     * @param endCellIndex   结束列号(从0开始)
     * @return 表格数据
     */
    private static List<List<CellData>> readExcelData(File templateFile, int startRowIndex, int endRowIndex, int startCellIndex, int endCellIndex) {
        ExcelReader excelReader = ExcelUtil.getReader(templateFile, 0);
        List<List<CellData>> rowDataList = new ArrayList<>();
        AtomicInteger rowIndex = new AtomicInteger(-1);
        excelReader.read(startRowIndex, endRowIndex, new CellHandler() {
            @Override
            public void handle(Cell cell, Object value) {
                if (cell == null) {
                    //无单元格跳过
                    return;
                }
                if (cell.getColumnIndex() < startCellIndex || cell.getColumnIndex() > endCellIndex) {
                    //列号不在范围内跳过
                    return;
                }

                //新行的数据
                if (cell.getRowIndex() != rowIndex.get()) {
                    rowDataList.add(new ArrayList<>());
                }
                rowIndex.set(cell.getRowIndex());
                //取出新行数据对象存储单元格数据
                List<CellData> cellDataList = rowDataList.get(rowDataList.size() - 1);
                CellData cellData = new CellData();
                cellData.setRowIndex(cell.getRowIndex());
                cellData.setCellIndex(cell.getColumnIndex());
                cellData.setValue(value);
                cellDataList.add(cellData);
            }
        });
        return rowDataList;
    }
}

 

3.表格

 

 

 

4.输出结果

[
    [
        {
            "cellIndex": 1,
            "rowIndex": 2,
            "value": "净资产"
        },
        {
            "cellIndex": 2,
            "rowIndex": 2,
            "value": 10000
        },
        {
            "cellIndex": 3,
            "rowIndex": 2,
            "value": " "
        },
        {
            "cellIndex": 4,
            "rowIndex": 2,
            "value": 1
        }
    ],
    [
        {
            "cellIndex": 1,
            "rowIndex": 3,
            "value": "市值"
        },
        {
            "cellIndex": 2,
            "rowIndex": 3,
            "value": 20000
        },
        {
            "cellIndex": 4,
            "rowIndex": 3,
            "value": 2
        }
    ],
    [
        {
            "cellIndex": 1,
            "rowIndex": 4,
            "value": "标题"
        }
    ],
    [
        {
            "cellIndex": 1,
            "rowIndex": 5,
            "value": "净利润"
        },
        {
            "cellIndex": 2,
            "rowIndex": 5,
            "value": 1000
        },
        {
            "cellIndex": 4,
            "rowIndex": 5,
            "value": 3
        }
    ]
]

 

标签:JAVA,rowIndex,Excel,hutool,value,cell,cellIndex,int,public
来源: https://www.cnblogs.com/hdwang/p/15774601.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有