<span style="white-space:pre"> </span>/**
* 读取商家列表xls文件
* @param filePath-xls文件路径
* @return 商家的List集合
*/
public static List<Store> readData(String filePath) {
// 用来存储商家
List<Store> list = new ArrayList<Store>();
try {
File file = new File(filePath);
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
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()返回该页的总行数
// 这里从1而不是从0开始循环, 因为第一行是标题
for (int i = 1; i < sheet.getRows(); i++) {
Store store = new Store();
// sheet.getColumns()返回该页的总列数
for (int j = 0; j < sheet.getColumns(); j++) {
String cellinfo = sheet.getCell(j, i).getContents();
System.out.println(j + cellinfo);
switch(j){
case 0 : store.setId(Integer.parseInt(cellinfo));
case 1 : store.setName(cellinfo);
default : ;
}
}
list.add(store);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
之后调用
public static void main(String[] args) throws Exception {
List<Store> store = readData("file\\门店.xlsx");
for(Store s : store) {
System.out.println(s.getName());
}
}
Excel :
打印信息 :
所需要的jar:jxl.jar