1.创建文件流,打开EXCEL文件(jxi不支持.xlsx文件,支持.xls)

FileInputStream excelFile = new FileInputStream(excelPath);
Workbook workbook = Workbook.getWorkbook(excelFile);

2.切换到对应文件名

Sheet excelSheet = workbook.getSheet(sheetName);

3.获取实际行数和列数

int rows = excelSheet.getRows();//行数
Cell[] cell = excelSheet.getRow(0);// 获得第一行的所有单元格
int columnNum = cell.length; // 单元格的个数 值 赋给 列数

4.读取数据

public static String ReadData(Sheet excelSheet, int row, int col){
try{
String CellData= "";
Cell cell = excelSheet.getRow(row)[col];
CellData = cell.getContents().toString();
return CellData;
}catch(Exception e){
return "";
}
}

示例:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class jxlExcel { public static void main(String[] args) throws IOException {
String excelPath = "F:\\login.xls";
String sheetName = "001";
try{
FileInputStream excelFile = new FileInputStream(excelPath);
Workbook workbook = Workbook.getWorkbook(excelFile);
Sheet excelSheet = workbook.getSheet(sheetName);
int rows = excelSheet.getRows();//行数
Cell[] cell = excelSheet.getRow(0);// 获得第一行的所有单元格
int columnNum = cell.length; // 单元格的个数 值 赋给 列数 for(int row = 0;row< rows; ++row){
for (int col =0; col < columnNum; ++col){
System.out.print(ReadData(excelSheet, row, col) + ' ');
if(col ==1)
System.out.println();
}
}
workbook.close();
}catch (FileNotFoundException e ){
System.out.println("找不到该文件");
} catch (BiffException e) {
e.printStackTrace();
}
} public static String ReadData(Sheet excelSheet, int row, int col){
try{
String CellData= "";
Cell cell = excelSheet.getRow(row)[col];
CellData = cell.getContents().toString();
return CellData;
}catch(Exception e){
return "";
}
}
}

最新文章

  1. [原创]zepto打造一款移动端划屏插件
  2. 为什么要重写hashcode() 方法
  3. [转]Snappy压缩库安装和使用之一
  4. iOS开发小技巧--图片的圆角处理
  5. Linux_MySql安装
  6. Octopus系列之模板快速开发手册
  7. 夺命雷公狗---node.js---1node的下载和安装
  8. Hive 7、Hive 的内表、外表、分区
  9. NGUI出现Shader wants normals, but the mesh UIAtlas doesn&amp;#39;t have them
  10. 【LeetCode】171. Excel Sheet Column Number
  11. 微信小程序swiper标签的测试
  12. spine - unity3D(摘自博主softimagewht)
  13. C语言博客作业03--函数
  14. PAT 之 A+B和C
  15. &gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt; [ovs][libvirt] virt-xml ovs-vsctl
  16. 厉害了,PS大神真的能改变世界!
  17. Java8新特性 -- Lambda基础语法
  18. 【事件流】浅谈事件冒泡&amp;&amp;事件捕获------【巷子】
  19. 用python脚本计算某一个文件的行数
  20. mem_fun与mem_fun_ref的区别

热门文章

  1. Linux如何修改命令提示符
  2. canvas绘制进度条(wepy)
  3. Spring的注解@Qualifier(二十五)
  4. python的单元测试代码编写流程
  5. linux内存查看工具
  6. Java SE 8 流库(一)
  7. Jmeter-配置元件
  8. Codeforces 148B: Escape
  9. 《DSP using MATLAB》示例Example 10.4
  10. [BZOJ3470]Freda’s Walk