java语言使用HutoolPoi读取excel

本文介绍了一个Java工具类ReadExcelUtil,演示如何通过指定路径、表索引、行数和列数读取Excel文件,支持首行为字段名的情况。它利用Apache POI库,处理空字段名列表和数据转换,返回JSONArray格式的数据对象。
摘要由CSDN通过智能技术生成
            读取指定行、列(list为空代表excel首为字段名)数据
public class ReadExcelUtil {

    /**
     * 读excel
     * @param pathName: 文件路径;sheetNum:表索引;rowNum:行数;colNum列数;list:传递当前的字段名,不传代表从excel读取
     * @return 数组对象
     */
    public static JSONArray readExcel(String pathName, Integer sheetNum, Integer rowNum, Integer colNum, List<String> list){
        rowNum--;//获得当前的 行和列索引
        colNum--;
        JSONArray json = new JSONArray();
        //jdk1.8的新特性
        try (
                FileInputStream fileInputStream = new FileInputStream(pathName);
                Workbook workbook = WorkbookFactory.create(fileInputStream);
        ) {
            Sheet sheet = workbook.getSheetAt(sheetNum);
            //读取总共有多少行数据
            int rowcount = sheet.getPhysicalNumberOfRows() + rowNum;
            //判断字段名是否存在,为空表示excel中 头行数据为字段名;有值表示 excel为纯数据,list中为当前字段名
            if (CollectionUtil.isEmpty(list)) {
                //获取字段名
                Row row = sheet.getRow(rowNum);
                if (row != null) {
                    //获取总共有多少列
                    int cellncount = row.getPhysicalNumberOfCells() + colNum;
                    list = new ArrayList<String>();
                    //从索引列开始读 读取数据 赋值给集合字段
                    for (int cellnum = colNum; cellnum < cellncount; cellnum++) {
                        Cell cell = row.getCell(cellnum);
                        if (cell != null) {
                            String stringCellValue = cell.getStringCellValue();
                            list.add(cellnum - colNum,stringCellValue);
                        }
                    }
                }
                rowNum++;
            }
            //从当前行索引开始读
            for (int rownum = rowNum; rownum < rowcount; rownum++) {
                JSONObject map = new JSONObject();
                Row rowData = sheet.getRow(rownum);
                if (rowData != null) {
                    //读取总列数 ,并从列索引开始 读取单元格数据 并使用 hutool工具包 CellUtil 进行类型匹配
                    int cellcount = rowData.getPhysicalNumberOfCells() + colNum;
                    for (int celnum = colNum; celnum < cellcount; celnum++) {
                        Cell cell = rowData.getCell(celnum);
                        if (cell != null) {
                            Object cellValue = CellUtil.getCellValue(cell);
                            //满足日期 进行 日期格式转换
                            if (cellValue instanceof DateTime){
                                DateTime d = (DateTime)cellValue;
                                String s = d.toString();
                                cellValue = s;
                            }
                            map.put(list.get(celnum - colNum), cellValue);
                        }
                    }
                }
                json.add(map);
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json;
    }

}
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值