常用方法記錄:java讀取Excel
阿新 • • 發佈:2019-01-27
簡單記錄一下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");
}