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