1.導入依賴JAR包
<!-- jxl 操作excel --> <dependency> <groupId>org.jxls</groupId> <artifactId>jxls-jexcel</artifactId> <version>1.0.6</version> </dependency>
2.創建工作簿並載入excel文件流
jxl.Workbook wb =null; InputStream is = new FileInputStream("D://G201.xls"); wb = Workbook.getWorkbook(is);
3.通過索引或者名稱獲取Sheet
wb.getSheet(0);
4.通過行號獲取Cell
sheet.getRow(j)
5.示例代碼展示
package com.bret.test; import jxl.Cell; import jxl.Sheet; import jxl.Workbook; import jxl.read.biff.BiffException; import org.junit.Test; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; public class ExcelUtil { @Test public void test(){ try{ jxl.Workbook wb =null; InputStream is = new FileInputStream("D://G201.xls"); wb = Workbook.getWorkbook(is); int sheetSize = wb.getNumberOfSheets(); Sheet sheet = wb.getSheet(0); int row_total = sheet.getRows(); for (int j = 0; j < row_total; j++) { if(j == 0){ Cell[] cells = sheet.getRow(j); System.out.println(cells[0].getContents()); System.out.println(cells[1].getContents()); System.out.println(cells[2].getContents()); } } }catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (BiffException e){ e.printStackTrace(); } } }
6.EXCEL內容
7.運行結果
注意,JAVA讀取exlcel僅支持.xls(97-2003工作簿),讀取后綴為.xlsx前需將文件另存為.xls。