//poi-ooxml poi
public String[] readExcel(File file) throws Exception{
InputStream inputStream = new FileInputStream(file);
String fileName = file.getName();
List<String> list = new ArrayList<String>();
Workbook wb = null;
if(fileName.endsWith("xls")){
wb = new HSSFWorkbook(inputStream);//解析xls格式
}else if(fileName.endsWith("xlsx")){
wb = WorkbookFactory.create(inputStream);//解析xlsx格式
}
Sheet sheet = wb.getSheetAt(0);//第一个工作表
int firstRowIndex = sheet.getFirstRowNum()+1;
int lastRowIndex = sheet.getLastRowNum();
for(int rIndex = firstRowIndex; rIndex <= lastRowIndex; rIndex ++){
Row row = sheet.getRow(rIndex);
if(row != null){
Cell cell = row.getCell(0);
String value = "";
if(cell != null && !"".equals(cell.toString())){
value = cell.toString();
list.add(value);
}
}
}
String[] staticType = list.toArray(new String[list.size()]);
return staticType;
}
转载于:https://my.oschina.net/u/1246433/blog/411168