java 读取 Excel 表格数据的工具类

工具类

public static List<Map<String, String>> readExcel(String filepath) {
    List<Map<String, String>> sheetList = new ArrayList<Map<String, String>>();
    try {
        //获取文件类型
        String fileType = filepath.substring(filepath.lastIndexOf(".") + 1, filepath.length());
        InputStream is = new FileInputStream(new File(filepath));
        Workbook wb = null;
        if (fileType.endsWith("xlsx")) {
            //Excel 2007
            wb = new XSSFWorkbook(is);
        } else if (fileType.endsWith("xls")) {
            //Excel 2003
            wb = new HSSFWorkbook(is);
        } else {
            throw new Exception("读取的不是excel文件");
        }
        //获取Excel的第一个sheet页面数据
        Sheet sheet = wb.getSheetAt(0);

        List<String> titles = new ArrayList<String>();
        //当前页面所有行的总数, 去掉+1为当前工作表的最后一行的行号
        int rowSize = sheet.getLastRowNum() + 1;
        //此时j为控制从第几行开始读取数据(避免前几行为表名等字段)
        for (int j = 1; j < rowSize-1; j++) {
            //读取Excel中第 j 行
            Row row = sheet.getRow(j);
            if (row == null) {
                continue;
            }
            //是获取最后一个不为空的列是第几个,比最后一列列标大1
            int cellSize = row.getLastCellNum();
            //此处比较的值需与j的初始值一致
            if (j == 1) {
                for (int k = 0; k < cellSize; k++) {
                    //获取第一行单元格数据,即对应的表头(数据库字段)
                    Cell cell = row.getCell(k);
                    titles.add(cell.toString());
                }
            } else {
                Map<String, String> rowMap = new HashMap<String, String>();
                //获取每一行表头字段对应的值,此k为第几列
                for (int k = 1; k < titles.size(); k++) {
                    Cell cell = row.getCell(k);
                    String key = titles.get(k);
                    String value = null;
                    if (cell != null) {
                        value = cell.toString();
                    }
                    //将字段名与值组成键值对存到map中
                    rowMap.put(key, value);
                }
                sheetList.add(rowMap);
            }
        }
        //释放资源
        wb.close();
        is.close();
        titles.clear();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return sheetList;
  }

测试运行

   public static void main(String [] args)  {
        String filepath =  "F:\\test.xlsx";
        List<Map<String, String>> sheetList = readExcel(filepath);
        System.out.println(sheetList.toString());
        for (Map<String, String> map :sheetList){
            String code = map.get("CODE");
            String name = map.get("NAME");
            System.out.println("CODE + " +code + ",NAME + "+name );
        }
    }
    

运行结果

在这里插入图片描述
测试用表格数据
在这里插入图片描述

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值