public ArrayList<String> readExcel(String filepath)
{
File file = new File(filepath);
ArrayList<String> list = new ArrayList<String>();
try {
// 创建输入流,读取Excel
InputStream is = new FileInputStream(file.getAbsolutePath());
// jxl提供的Workbook类
Workbook wb = Workbook.getWorkbook(is);
// Excel的页签数量
int sheet_size = wb.getNumberOfSheets();
for (int index = 0; index < sheet_size; index++) {
//获得页签名称
String sheetnames[] = wb.getSheetNames();
// 每个页签创建一个Sheet对象
Sheet sheet = wb.getSheet(index);
// sheet.getRows()返回该页的总行数
//第一行列名称,不读取
for (int i = 1; i < sheet.getRows(); i++) {
StringBuffer sb = new StringBuffer();
for(int j=0; j < sheet.getColumns();j++){
sb.append(sheet.getCell(j, i).getContents());
sb.append(",");
//System.out.println(sb.toString());
}
list.add(sb.toString());
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (BiffException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return list;
} |
|