常用方法记录:java读取Excel

来源:互联网 发布:linux创建物理卷 编辑:程序博客网 时间:2024/05/22 17:15

简单记录一下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");    }
原创粉丝点击