读取execl表格

读取excel文件,首先呢需要去读取excel的表单数量,然后根据去读取表单里面的内容,并且输出到控制台

    //文件地址  也可以放在main方法里面
    public static String PATH = "D:\\test\\1667194328379.xlsx";

    private static void ReadExcel(String url) throws Exception {
        InputStream inputStream = new FileInputStream(url);
        Workbook workbook = WorkbookFactory.create(inputStream);

        int numberOfSheets = workbook.getNumberOfSheets();
        //获取表单数量
        System.out.println("numberOfSheets="+numberOfSheets);

        Sheet sheet = workbook.getSheetAt(0);
        int i1 = readExcelValueRows(sheet);
        System.out.println(i1);
        //获取sheet的行数
        int rows = sheet.getPhysicalNumberOfRows();
        System.out.println(rows);
        for (int i = 0; i < rows; i++) {
            //过滤表头行
            if (i == 0) {
                continue;
            }
            //获取当前行的数据
            Row row = sheet.getRow(i);
            if (row==null){
                break;
            }
            int index = 0;
            for (Cell cell : row) {
                System.out.println(cell);
            }

        }
    }
    /**
     * 用来得到真实行数
     * @param sheet 需要读取的Excel表格(excel文件的工作簿的名称)
     * @return
     *
     */
    public static int readExcelValueRows(Sheet sheet) {
        int realRow = 0;// 返回的真实行数
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            //i从1开始,不判断第一行标题行
            Row row = sheet.getRow(i);
            if (row == null){
                continue;
            }
            for (Cell cell : row) {
                if (cell == null){
                    continue;
                }
                String value = cell.getStringCellValue();
                if (value == null || "".equals(value)){
                    continue;
                } else{
                    realRow++;
                    break;
                }
            }
        }
        return realRow;
    }

    public static void main(String[] args) throws Exception {
        ReadExcel(PATH);
    }

输出
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值