HSSFWorkbook:是操作Excel2003以前(包括2003)的版本,扩展名是.xls;
XSSFWorkbook:是操作Excel2007的版本,扩展名是.xlsx;
1.注意点
getPhysicalNumberOfRows()获取的是物理行数,也就是不包括那些空行(隔行)的情况(编号从1开始)。 getLastRowNum()获取的是最后一行的编号(编号从0开始)。 getPhysicalNumberOfCells 是获取不为空的列个数。 getLastCellNum 是获取最后一个不为空的列是第几个。
eg.
我使用getLastRowNum()获取行数,getLastCellNum获取列数:
Excel中 有12行,11列。
int rows = sheet.getLastRowNum(); //获取的是最后一行的编号(编号从0开始)
System.out.println("rows=="+rows); //rows==11
int cells = sheet.getRow(startrow).getLastCellNum();
System.out.println("cells=="+cells); //cells==11
------------------------------------------------------------------------------------------------------
CELL_TYPE_NUMERIC 数值型 0
CELL_TYPE_STRING 字符串型 1
CELL_TYPE_FORMULA 公式型 2
CELL_TYPE_BLANK 空值 3
CELL_TYPE_BOOLEAN 布尔型 4
CELL_TYPE_ERROR 错误 5
2.HSSFWorkbook的关键代码
/** * 读取后缀名为xls的Excel */ public static List<Map<String,String>> readExcel2003(String filename) throws FileNotFoundException, IOException{ int startrow = 0; //第一行开始读取 List<String> columnNameList = new ArrayList<String>(); List<Map<String,String>> list = new ArrayList<Map<String,String>>(); HSSFWorkbook wookbook = new HSSFWorkbook(new FileInputStream(new File(filename))); HSSFSheet sheet = wookbook.getSheetAt(0); int rows = sheet.getLastRowNum(); //获取的是最后一行的编号(编号从0开始) System.out.println("rows=="+rows); int cells = sheet.getRow(startrow).getLastCellNum(); System.out.println("cells=="+cells); for (int i=0; i < (rows+1); i++) { //获取的是最后一行的编号(编号从0开始)。所以rows+1 HSSFRow row = sheet.getRow(i); if (row != null) { if (i==startrow) { //获取名称 for (int j=0; j<cells; j++) { HSSFCell cell = row.getCell(j); if (cell != null && !cell.getStringCellValue().equals("")) { columnNameList.add(cell.getStringCellValue()); System.out.println(j + ":" + cell.getStringCellValue()); }else { cells=j; //cells为size所以从1开始 break; } } } else { Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < cells; j++) { HSSFCell cell = row.getCell(j); if (cell != null) { map.put(columnNameList.get(j), cell.getStringCellValue()); }else{ map.put(columnNameList.get(j), null); } } list.add(map); } } } return list; }
3.XSSFWorkbook的关键代码
/** * 读取后缀名为xlsx的Excel */ public static List<Map<String,String>> readExcel2007(String filename) throws FileNotFoundException, IOException{ int startrow = 0; //第一行开始读取 List<String> columnNameList = new ArrayList<String>(); List<Map<String,String>> list = new ArrayList<Map<String,String>>(); XSSFWorkbook workbook = new XSSFWorkbook(new FileInputStream(new File(filename))); XSSFSheet sheet = workbook.getSheetAt(0); int rows = sheet.getLastRowNum(); // System.out.println("rows:"+ rows); int cells = sheet.getRow(startrow).getLastCellNum(); for (int i=0; i < (rows+1); i++) { //获取的是最后一行的编号(编号从0开始)。所以rows+1 XSSFRow row = sheet.getRow(i); if (row != null) { if(i == startrow) { //获取名称 for (int j=0; j<cells; j++) { XSSFCell cell = row.getCell(j); if (cell != null && !cell.getStringCellValue().equals("")) { columnNameList.add(cell.getStringCellValue()); }else { cells=j; //cells为size所以从1开始 break; } } }else { Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < cells; j++) { XSSFCell cell = row.getCell(j); if (cell != null) { switch (cell.getCellType()) { case HSSFCell.CELL_TYPE_FORMULA: case HSSFCell.CELL_TYPE_NUMERIC: boolean flag = HSSFDateUtil.isCellDateFormatted(cell); if(flag){ SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); Date date = cell.getDateCellValue(); String result = sdf.format(date); map.put(columnNameList.get(j), result); }else{ String str = String.valueOf(cell.getNumericCellValue()); str = str.substring(0, str.length()-2); map.put(columnNameList.get(j), str); } break; case HSSFCell.CELL_TYPE_STRING: map.put(columnNameList.get(j), cell.getStringCellValue()); break; default: map.put(columnNameList.get(j), null); break; } }else { map.put(columnNameList.get(j), null); } } list.add(map); } } } return list; }