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 "";
    }
}
相关推荐
Fanxt_Ja3 天前
【LeetCode】算法详解#15 ---环形链表II
数据结构·算法·leetcode·链表
今后1233 天前
【数据结构】二叉树的概念
数据结构·二叉树
凯子坚持 c3 天前
精通 Redis list:使用 redis-plus-plus 的现代 C++ 实践深度解析
c++·redis·list
路由侠内网穿透3 天前
本地部署 GPS 跟踪系统 Traccar 并实现外部访问
运维·服务器·网络·windows·tcp/ip
第七序章3 天前
【C++STL】list的详细用法和底层实现
c语言·c++·自然语言处理·list
研华嵌入式3 天前
如何在高通跃龙QCS6490 Arm架构上使用Windows 11 IoT企业版?
arm开发·windows·嵌入式硬件
散1123 天前
01数据结构-01背包问题
数据结构
消失的旧时光-19433 天前
Kotlinx.serialization 使用讲解
android·数据结构·android jetpack
Gu_shiwww3 天前
数据结构8——双向链表
c语言·数据结构·python·链表·小白初步
带娃的IT创业者3 天前
Windows 平台上基于 MCP 构建“文心一言+彩云天气”服务实战
人工智能·windows·文心一言·mcp