jxl 读取2003 excel 示例
package com.hkrt.excel;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class ReadExcelDemo {
public static void main(String[] args) throws BiffException, IOException {
List<Student> resultList = readExcel("G:\\student.xls");
for (int i =resultList.size()-1; i >=0; i--) {
System.out.println(resultList.get(i));
}
}
/**读取excel 2003 */
public static List<Student> readExcel(String excelFileName) throws BiffException,IOException {
List<Student> stus = new ArrayList<Student>();// 创建一个list 用来存储读取的内容
Workbook rwb = null;
Cell cell = null;
InputStream stream = new FileInputStream(excelFileName);// 创建输入流
rwb = Workbook.getWorkbook(stream); // 获取Excel文件对象
Sheet sheet = rwb.getSheet(0);// 获取文件的指定工作表 默认的第一个
for (int i = 1; i < sheet.getRows(); i++) { // 行数(表头的目录不需要,从1开始)
String[] str = new String[sheet.getColumns()];// 创建一个数组 用来存储每一列的值
for (int j = 0; j < sheet.getColumns(); j++) {// 列数
cell = sheet.getCell(j, i);// 获取第i行,第j列的值
str[j] = cell.getContents();
}
Student stu = new Student(str); // 把刚获取的列存入list
stus.add(stu);
}
return stus;
}
}
public class Student {
private int id;
private String name;
private String age;
private String grade;
public Student() {
}
public Student(String[] str) {
this.id = Integer.valueOf(str[0]);
this.name = str[1];
this.age = str[2];
this.grade = str[3];
}}
excel.xls
编号 姓名 年龄 班级
1 张三 8 1
2 李四 6 3

浙公网安备 33010602011771号