1,下载jxl.jar包(百度jxl.jar就能找到),在项目中加载jxl.jar包
2,声明Sheet、Workbook和Cell实例变量;Sheet用来得到工作表中的数据,Workbook用来加载表格文件,Cell用来存储每一格的数据
3,Workbook有一个静态函数Workbook.getWorkbook(new File(path));得到Workbook实例
sheet.getCell(int i,intj);获取(i,j)的表格数据并存储在Cell实例中
public static void main(String[] args) {
// TODO Auto-generated method stub
int i;
Sheet sheet;
Workbook book;
Cell cell1,cell2,cell3;
try {
//作为一个t.xls文件的读取
book=Workbook.getWorkbook(new File("D:\\w.xls"));
//获取第一个工作表对象(excel中sheet的编号从0开始
sheet=book.getSheet(0);
cell1=sheet.getCell(0,0);
System.out.println("标题为"+cell1.getContents());
i=1;
while(true) {
cell1=sheet.getCell(0,i);//列,行
cell2=sheet.getCell(1, i);
cell3=sheet.getCell(2, i);
if("".equals(cell1.getContents())==true)
break;
System.out.println(cell1.getContents()+"\t"+cell2.getContents()+"\t"+cell3.getContents());
i++;
}
book.close();
}catch(Exception e) {
e.printStackTrace();
}
}
*注意:这里只能读取2003-2007版本的excel数据,对于后置为.xlsx的文件不支持读取,会有下面的异常
jxl.read.biff.BiffException: Unable to recognize OLE stream
at jxl.read.biff.CompoundFile.<init>(CompoundFile.java:116)
at jxl.read.biff.File.<init>(File.java:127)
at jxl.Workbook.getWorkbook(Workbook.java:221)
at jxl.Workbook.getWorkbook(Workbook.java:198)
at dream.ExcelTest.main(ExcelTest.java:19)
来源:https://blog.csdn.net/weixin_44833716/article/details/100545561