/**
* Read out all data information in
filePath* @param filePath absolute path of excel file
*
*/
public static void getDataFromExcel(String filePath){ List<Map<String,Integer>> list = new ArrayList<Map<String, Integer>>(); / / Determine whether it is an excel type file if(!filePath.endsWith(".xls")&&!filePath.endsWith(".xlsx")){ System.out.println("The file is not of excel type"); } FileInputStream fis =null; Workbook wookbook = null; int flag = 0; try{ //Get a stream with an absolute address fis = new FileInputStream(filePath); }catch(Exception e){ e.printStackTrace (); } try{ //2003 version of excel, ending with .xls wookbook = new HSSFWorkbook(fis);//Get the workbook }catch (Exception ex){ try{ fis = new FileInputStream(filePath); //2007 version of excel, ending with .xlsx wookbook = new XSSFWorkbook(filePath);//Get the workbook } catch (IOException e){ // TODO Auto-generated catch block e.printStackTrace (); } } // get a worksheet Sheet sheet = wookbook.getSheetAt(0); //get header Row rowHead = sheet.getRow(0); //Place different headers according to different data Map<Object,Integer> headMap = new HashMap<Object, Integer>(); //Determine whether the header is qualified ------------------------ here to see how many columns you have if(rowHead.getPhysicalNumberOfCells() != 2){ System.out.println("The number of header columns does not correspond to the database to be imported"); } try{ //---------------- here according to how many columns your table has while (flag < 2){ Cell cell = rowHead.getCell(flag); if (getRightTypeCell(cell).toString().equals("姓名")){ headMap.put("personName", flag); } if (getRightTypeCell(cell).toString().equals("性别")){ headMap.put("sex", flag); } flag++; } } catch (Exception e){ e.printStackTrace (); System.out.println("The header is not standard, please re-import after modification"); } //get the total number of rows of data int totalRowNum = sheet.getLastRowNum(); // to get the property String name = ""; String sex = ""; List<PiousRegister> listPious = new ArrayList<PiousRegister>(); List<PiousInfo> listPiousInfo = new ArrayList<PiousInfo>(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); if(0 == totalRowNum){ System.out.println("There is no data in Excel!"); } Cell cell_1 = null,cell_2 = null; // get all data for(int i = 1 ; i <= totalRowNum ; i++){ // get the object in row i Row row = sheet.getRow(i); try{ cell_1 = row.getCell(headMap.get("personName")); cell_2 = row.getCell(headMap.get("sex")); } catch (Exception e){ e.printStackTrace (); System.out.println("Get cell error"); } try{ if(cell_1 != null && cell_2 != null){ PiousRegister piousReg = new PiousRegister(); name = (String) getRightTypeCell(cell_1); piousReg.setPersonName (name); sex = (String) getRightTypeCell(cell_2); piousReg.setPersonSex(sex.equals("男") ? 0 : 1); System.out.println("Name: "+name+", Gender: "+sex); } } catch (ClassCastException e){ e.printStackTrace (); System.out.println("The data is not all numbers or all text!"); } } } /** * * @param cell an object of a cell * @return returns the value of the corresponding type of the cell */ public static Object getRightTypeCell(Cell cell){ DecimalFormat df = new DecimalFormat("#"); Object object = null; switch(cell.getCellType()){ case Cell.CELL_TYPE_STRING :{ object=cell.getStringCellValue(); break; } case Cell.CELL_TYPE_NUMERIC :{ if(HSSFDateUtil.isCellDateFormatted(cell)){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); return sdf.format(HSSFDateUtil.getJavaDate(cell.getNumericCellValue())).toString(); } return df.format(cell.getNumericCellValue()); } case Cell.CELL_TYPE_FORMULA :{ cell.setCellType(Cell.CELL_TYPE_NUMERIC); object=cell.getNumericCellValue(); break; } case Cell.CELL_TYPE_BLANK :{ cell.setCellType(Cell.CELL_TYPE_BLANK); object=cell.getStringCellValue(); break; } } return object; }