java中用jxl方式读取Excel
2013-11-10 19:33
417 查看
首先导入jxl的包,可以在网上下载
例如 读取一份存储有学生信息的Excel
public static List<Student> readExcel(String filePath){
List<Student> students = new ArrayList<Student>();
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(filePath);
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
for (int i = 0; i < sheet.getRows(); i++) { //代表行
// sheet.getColumns()返回该页的总列数
Student student = new Student();
//第一行为字段,所以跳过
if(i==0){
continue;
}
for (int j = 0; j < sheet.getColumns(); j++) { //代表列
String cellinfo = sheet.getCell(j, i).getContents();
int m = sheet.getCell(j, i).getColumn();
switch(m){
case 0:
student.setNumber(Integer.parseInt(cellinfo));
break;
case 1:
student.setName(cellinfo);
break;
case 2:
student.setSex(cellinfo);
break;
case 3:
student.setAge(Integer.parseInt(cellinfo));
break;
case 4:
student.setAddress(cellinfo);
break;
}
// System.out.println(cellinfo);
}
students.add(student);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(students.size());
return students;
}
例如 读取一份存储有学生信息的Excel
public static List<Student> readExcel(String filePath){
List<Student> students = new ArrayList<Student>();
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(filePath);
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
for (int i = 0; i < sheet.getRows(); i++) { //代表行
// sheet.getColumns()返回该页的总列数
Student student = new Student();
//第一行为字段,所以跳过
if(i==0){
continue;
}
for (int j = 0; j < sheet.getColumns(); j++) { //代表列
String cellinfo = sheet.getCell(j, i).getContents();
int m = sheet.getCell(j, i).getColumn();
switch(m){
case 0:
student.setNumber(Integer.parseInt(cellinfo));
break;
case 1:
student.setName(cellinfo);
break;
case 2:
student.setSex(cellinfo);
break;
case 3:
student.setAge(Integer.parseInt(cellinfo));
break;
case 4:
student.setAddress(cellinfo);
break;
}
// System.out.println(cellinfo);
}
students.add(student);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.print(students.size());
return students;
}
相关文章推荐
- java 初始化与清理
- JDK源码学习之一lang包
- struts里的配置action的scope属性
- eclipse 安装designer
- Ubuntu 13.10 eclipse菜单栏失效
- java项目web.xml文件报错
- Java中判断String不为空的问题
- Spring DI的配置使用
- java中的抽象类(abstract class)
- Javacard技术总结之--- 消息摘要算法
- 捕获javaw的输出
- java学习笔记-----面向对象
- SDL游戏开发教程01(环境准备 mingw+eclipse)
- MyEclipse10搭建安卓环境
- Java单例模式的线程安全问题
- Java线程的终止和同步
- Java开发者的十大戒律
- 疯狂java 邮箱分享
- java集合体系
- Eclipse开发环境的使用问题