1. 程式人生 > 其它 >JAVA使用hutool poi工具讀取Excel表格指定行列範圍的資料

JAVA使用hutool poi工具讀取Excel表格指定行列範圍的資料

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
        }
    ]
]