hutool 读取每个sheet,数据转成List<Map<>>的格式

1.接收上传的excel文件流,取出第一个sheet

复制代码
@ApiOperation("【干部管理】根据excel导入干部和企业")
    @PostMapping("/importExcel")
    @Transactional
    public Result importExcel(@RequestParam MultipartFile file) throws IOException {
        Logger logger = LoggerFactory.getLogger(getClass());
        ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
        List<Sheet> sheets = reader.getSheets();
        Sheet rows = sheets.get(0);
        List<Map<String, String>> sheetData = ImportExcelUtil.getSheetData(rows);
        
        return null;
    }

2.ImportExcelUtil 工具类

复制代码
package com.enterprise.util.excel;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;

import java.math.BigDecimal;
import java.util.*;

//读取excel工具类
public class ImportExcelUtil {
    /**
     * 读取Sheet 的所有数据
     * @param rows
     * @return
     */
    public static List<Map<String, String>> getSheetData(Sheet rows){
        Map<Integer, String> tableHeader=new HashMap<Integer, String>();
        List<Map<String, String>> list=new ArrayList<Map<String, String>>();

        int rowNum=0;
        Iterator<Row> rowIterator = rows.rowIterator();
        while (rowIterator.hasNext()){

            Row row = rowIterator.next();
            Iterator<Cell> cellIterator = row.cellIterator();
            int column=0;
            Map<String, String> tdata=new HashMap<>();
            while(cellIterator.hasNext()){
                Cell next = cellIterator.next();
                int columnIndex = next.getColumnIndex();
                String cellValue = getCellValue(next);
                if(rowNum == 0){
                    //第0行,表头
                    tableHeader.put(column,cellValue);
                }else {
                    tdata.put(tableHeader.get(columnIndex),cellValue);
                }
                column++;
            }
            if(rowNum != 0){
                list.add(tdata);
            }
            rowNum ++;
        }
        return list;
    }

    private static String getCellValue(Cell cell) {
        CellType cellType = cell.getCellType();
        if (cellType == CellType.STRING){
            String stringCellValue = cell.getStringCellValue();
            return stringCellValue;
        }else if (cellType == CellType.NUMERIC){
            Double value = cell.getNumericCellValue();
            BigDecimal bd1 = new BigDecimal(Double.toString(value));
            // 去掉后面无用的零  如小数点后面全是零则去掉小数点
            String s = "";
            if (bd1.toPlainString().contains(".")){
                s = bd1.toPlainString().replaceAll("0+?$", "").replaceAll("[.]$", "");
            }else{
                s = bd1.toPlainString();
            }

            return s;
        }
        return "";
    }
}
相关推荐
kakakahahahaha7 小时前
Win10开始菜单打不开排查:explorer、AppX注册、SFC、服务与用户配置修复
windows·电脑·笔记本电脑·内容运营·软件需求
ITHAOGE158 小时前
下载 | Windows Server 2016最新纯净版ISO映像!(集成9月更新、标准版、数据中心版、14393.9514)
windows·科技·微软·电脑
余额瞒着我当琳9 小时前
红黑树的实现--红黑树的概念、规则、效率、结构、旋转、查找、验证
数据结构·c++·算法
程序员南飞9 小时前
win系统安装minio
java·数据结构·算法
w_zero_one10 小时前
链表(4)
java·数据结构·算法
白山编程大哥12 小时前
C语言篇:语法进阶
c语言·数据结构·算法
Logic10113 小时前
C语言/数据结构动态规划题解:爬楼梯(含坏台阶)——避开障碍物的路径计数
c语言·数据结构·动态规划·时间复杂度·空间复杂度·算法题·爬楼梯
传奇开心果编程14 小时前
【Compose Multiplatform 跨端开发学与练】第3课 布局与组件
android·windows·学习·ui·ios·kotlin·composer
All for pursuit.14 小时前
【回溯-6】79.单词搜索
数据结构·c++·算法·leetcode
Benny_Tang14 小时前
AT_arc180_d [ARC180D] Division into 3 题解
数据结构·c++·算法