import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ImportExcel {
public Workbook initWorkbook(String filePach) { Workbook workbook = null; try { workbook = new HSSFWorkbook(new FileInputStream(filePach)); } catch (Exception e1) { try { workbook = new XSSFWorkbook(new FileInputStream(filePach)); } catch (Exception e) { e.printStackTrace(); } } return workbook; } public Map<String, List<String>> transverterToMap(String filePath) throws Exception { Map<String, List<String>> result = new HashMap<String, List<String>>(); try { // 表头行 String[] titles = null; Sheet aSheet = initWorkbook(filePath).getSheetAt(0); for (int rowNumOfSheet = 0; rowNumOfSheet <= aSheet.getLastRowNum(); rowNumOfSheet++) { Row aRow = aSheet.getRow(rowNumOfSheet); if (titles == null) { titles = new String[aRow.getLastCellNum()]; } for (int cellNumOfRow = aRow.getFirstCellNum(); cellNumOfRow < aRow .getLastCellNum(); cellNumOfRow++) { Cell aCell = aRow.getCell(cellNumOfRow); aCell.setCellType(XSSFCell.CELL_TYPE_STRING); String cellStr = aCell.getStringCellValue(); if (isTitleRow(aRow)) { titles[cellNumOfRow] = cellStr; result.put(cellStr, new ArrayList<String>()); } else { List<String> list = result.get(titles[cellNumOfRow]); list.add(cellStr); } } aRow = null; } aSheet = null; } catch (Exception e) { throw e; } return result; } private boolean isTitleRow(Row row) { // 默认情况 excel首行为表头行 return row.getRowNum() == 0 ? true : false; } public static void main(String[] args) throws Exception { Map<String, List<String>> map = new ImportExcel() .transverterToMap("E:/upload/excel/temp/20120823/2_test.xlsx"); System.out.println(map); } }