常用方法记录:java读取Excel

简单记录一下java读取excel文件的小方法。

使用jxl.jar进行读取,需要注意的是,jxl.jar只能读取xls格式的excel

Maven引用:

        <dependency>
            <groupId>net.sourceforge.jexcelapi</groupId>
            <artifactId>jxl</artifactId>
             <version>2.5.7</version>
        </dependency> 

方法传入一个文件夹path,遍历该文件夹下所有excel文件,输出文件内容到控制台

    public static void readExcel(String path) throws BiffException, IOException, InterruptedException{
        File file = new File(path);
        if(!file2.getName().endsWith(".xls")){
                continue;
        }
        File[] files = file.listFiles();
        for (File file2 : files) {
            Sheet sheet = null;
            Workbook book = null;
            book = Workbook.getWorkbook(file2); 
            sheet = book.getSheet(0);
            for(int row = 0; row < sheet.getRows(); row++ ){
                for(int col = 0; col < sheet.getColumns(); col++){
                    String cellText = sheet.getCell(col, row).getContents();
                    if ("".equals(cellText)) {
                        continue;
                    }
                    System.out.println(cellText);
                }
            }
        }
    }

    public static void main(String[] args) throws BiffException, IOException, InterruptedException {
        readExcel("C:\\Users\\path");
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值