81,092
社区成员
发帖
与我相关
我的任务
分享
/**
* 传入excel文件路径 获取一个列表
* @param filePath Excel 文件路径
* @param isDelete 是否在读取完删除该文件
* @return
*/
public static List readExcel(String filePath) {
jxl.Workbook rwb = null;
InputStream is = null;
List<List<String>> biglist = new ArrayList<List<String>>();
try {
// 构建Workbook对象, 只读Workbook对象
// 直接从本地文件创建Workbook
// 从输入流创建Workbook
is = new FileInputStream(filePath);
rwb = Workbook.getWorkbook(is);
// Sheet(术语:工作表)就是Excel表格左下角的Sheet1,Sheet2,Sheet3但在程序中
// Sheet的下标是从0开始
// 获取第一张Sheet表
Sheet rs = rwb.getSheet(0);
// 获取Sheet表中所包含的总列数
int rsColumns = rs.getColumns();
// 获取Sheet表中所包含的总行数
int rsRows = rs.getRows();
// 获取指定单元格的对象引用
List<String> colnames = new ArrayList<String>();
for (int j = 0; j < rsColumns; j++) {
Cell cell = rs.getCell(j, 0);
colnames.add(cell.getContents());
}
biglist.add(colnames);
//**从第二行开始读取数据 这里默认从第2行开始读取
for (int i = 1; i < rsRows; i++) {
List<String> smalllist = new ArrayList<String>();
boolean flag = false;
for (int j = 0; j < rsColumns; j++) {
Cell cell = rs.getCell(j, i);
smalllist.add(cell.getContents());
if(cell.getContents() != null && !"".equals(cell.getContents())){
flag = true;
}
}
if(flag){
biglist.add(smalllist);
}
}
return biglist;
} catch (Exception e) {
e.printStackTrace();
} finally {
if(rwb!=null) {
rwb.close();
}
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return biglist;
}