用Eclipse读取excel中全部数据
引包:jxl.jar
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Vector;
import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
public class Excel {
public Vector ReadExcel(String file) {
List list = new ArrayList();
Vector vector=new Vector();
Workbook rwb = null;
Cell cell = null;
// 创建输入流
InputStream stream = null;
try {
stream = new FileInputStream(file);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获取Excel文件对象
try {
rwb = Workbook.getWorkbook(stream);
} catch (BiffException | IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 获取文件的指定工作表 默认的第一个
Sheet sheet = rwb.getSheet(0);
// 行数(表头的目录不需要,从1开始)
for (int i = 0; i < sheet.getRows(); i++) {
// 创建一个数组 用来存储每一列的值
String[] str = new String[sheet.getColumns()];
// 列数
for (int j = 0; j < sheet.getColumns(); j++) {
// 获取第i行,第j列的值
cell = sheet.getCell(j, i);
str[j] = cell.getContents();
}
list.add(str);
// 把刚获取的列存入list
}
for (int i = 0; i < list.size(); i++) {
String[] str = (String[]) list.get(i);
Vector c = null;
for (int j = 0; j < str.length; j=j+3) {
c=new Vector();
c.add(str[j]);
c.add(str[j+1]);
}
vector.add(c);
// System.out.println(vector);
}
return vector;//返回一个vector为每一列的数据
}
}