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 "";
    }
}
相关推荐
黄豆匿zlib2 分钟前
Python中的其他数据结构:除了列表和元组,还有哪些?
数据结构·windows·python
90wunch13 分钟前
更进一步深入的研究ObRegisterCallBack
c++·windows·安全
啾啾Fun31 分钟前
Python类型处理与推导式
开发语言·windows·python
Serendipity_筱楠1 小时前
Windows安装部署jenkins
windows·ci/cd·自动化·jenkins·测试
FL16238631291 小时前
[windows工具]PDFOCR识别重命名工具1.3 版本使用教程及注意事项
windows
midsummer_woo1 小时前
windows系统安全加固
windows·安全·系统安全
code bean1 小时前
【WPF】WPF 中 `DisplayMemberPath` 与 `SelectedValuePath` 的深入理解与实战应用
windows·wpf
Watermelo6173 小时前
内存泄漏到底是个什么东西?如何避免内存泄漏
开发语言·前端·javascript·数据结构·缓存·性能优化·闭包
Frank_zhou17 小时前
算法-链表实战【删除链表的倒数第 N 个结点】中等
数据结构
guiyanakaung19 小时前
一篇文章让你学会 Compose Multiplatform 推荐的桌面应用打包工具 Conveyor
android·windows·macos