POI读2003 EXCEL文件

本文介绍了一种从Excel 2003文件中读取数据的方法,包括处理文件流、解析工作簿、遍历表格数据及错误处理等步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public void readExcel2003()
    {
        
        // 先获取文件的流
        FileInputStream fis = null;
        try
        {
            String filePath = System.getProperty("user.dir")+File.separator+"config"+File.separator+"2003.xls";
            File file = new File("FilePath");
            
            if (!file.exists())
            {
                // 当文件不存在,则直接返回null,并记录日志
                loger.error("The file is not exist!");
                return ;
            }
            
            fis = new FileInputStream(filePath);
            // 得到EXCEL文件的workbook
            HSSFWorkbook workbook = new HSSFWorkbook(fis);
            // 得到EXCEL的sheet页,默认在第1个sheet页中
            HSSFSheet sheet = workbook.getSheetAt(0);
            // 得到sheet页中的总行数
            int rowNum = sheet.getLastRowNum();
            
            if (rowNum <= 0)
            {
                // 由于第一行是标题行,当sheet中的行数小于等于1的时候,表示该sheet中没有数据信息
                loger.error("The file has no source!");
                return ;
            }
            
            HSSFRow row = null; //行
            
            HSSFCell cell = null; // 列
            String value = "";
            
            
            // 因为第一行是标题,所以从第二行开始循环获取信息
            for (int i = 1; i <= rowNum; i++)
            {
                // 获得每一行数据
                row = sheet.getRow(i);
                
                if (null == row)
                {
                    // 当此行为null的时候继续下一轮循环
                    continue;
                }
                
                cell = row.getCell(0); //得到第1列
                if (null == cell)
                {
                    loger.info("第 " + i + " 行第一列没有数据!");
                    // 当第一列为空就需要进入下一轮循环
                    continue;
                }
                else
                {
                    value = cell.getStringCellValue().trim();
                    if (null == value || value.isEmpty())
                    {
                        // 当第一列中没有数据就需要进入下一轮循环
                        continue;
                    }
                    
                }
                
                
            }
            
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        finally
        {
            if (null != fis)
            {
                try
                {
                    //最后关闭FileInputStream资源
                    fis.close();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        }
    }

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值