程序默认跳过表头,从第二行数据开始读:
public List<String> readMobilesFromExcel(InputStream inputStream) throws Exception {
List<String> result = new ArrayList<String>();
POIFSFileSystem inputPoifsFileSystem = null;
try {
inputPoifsFileSystem = new POIFSFileSystem(inputStream);
HSSFWorkbook hssfWorkbook = new HSSFWorkbook(inputPoifsFileSystem);
HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(0);
int size = hssfSheet.getLastRowNum();
for (int i = 1; i <= size; i++) {
// 读取一行记录
HSSFRow hssfRow = hssfSheet.getRow(i);
if (hssfRow.getCell(0) == null || "".equals(hssfRow.getCell(0).getStringCellValue().trim())) {
// 为空的不处理
} else {
result.add(hssfRow.getCell(0).getStringCellValue().trim());
}
}
} catch (Exception e) {
throw e;
}finally{
inputStream.close();
}
return result;
}