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。