涉及的包:
org.apache.poi.hssf.usermodel.HSSFSheet;
org.apache.poi.hssf.usermodel.HSSFWorkbook;
org.apache.poi.hssf.usermodel.HSSFRow;
public Map<String,Object> initDataFromExcel(File file){
Map<String,Object> errorMap=new HashMap<String,Object>();
errorMap.put("false","false");
StringBuffer sb= new StringBuffer();
FileInputStream inputStream=new FileInputStream(file);
HSSFWorkbook workbook=new HSSFWorkbook(inputStream);
//获取sheet表
HSSFSheet sheet=workbook.getSheetAt(0);
HSSFRow row=null;
List<Map<String,Object>> ob=new ArrayList<Map<String,Object>>)();
for(int i=1,length=sheet.getLastRowNum();i<=length;i++) {
row=sheet.getRow(i);
List<Map<String,Object>> list=new ArrayList<Map<String,Object>>();
Map<String,Object> map=new HashMap<String,Object>();
//姓名
if(StringUtil.isBank(getCellValue(row.getCell(2)).toString())) {
}else {
String name=getCellValue(row.getCell(2)).toString();f
map.put("name", name);
}
if(StringUtil.isBlank(getCellValue(row.getCell(1)).toString())) {
}else {
String idCard =getCellValue(row.getCell(1)).toString;
sb.append(idCard);
sb.append(",");
map.put("idCard", idCard);
}
ob.add(map);
}
Map<String,Object> personInfo=new HashMap<String,Object>();
personInfo.put("param", ob);
Map<String,Object> idCardStr=new HashMap<String,Object>();
String idCards=sb.toString();
if(StringUtil.isNotBlank(idCards)) {
idCards=idCards.substring(0,idCards.length()-1);
idCardStr.put("idCards", idCards);
personInfo.put("idCardStr", idCardStr);
return personInfo;
}
}
private Object getCellValue(Cell cell){
Object value =null;
//字符
if(cell !=null){
value = cell.toString();
}else{
value=" ";
}
return value;
}