天天看点

2018-07-03 根据Excel后缀名获取WorkBook

需求:根据Excel后缀名获取WorkBook,兼容Excel2003和Excel2007

public Workbook getWorkBook( File file){ Workbook workbook = null; InputStream is = null; if (file.exists()) { try { //1.获取文件输入流 is = new FileInputStream(file); //2.获取文件名称 String fileName = file.getName(); if (StringUtils.isNotBlank(fileName)) { if (fileName.endsWith( ".xlsx")) { workbook = new XSSFWorkbook(is); // Excel 2003 } else if (fileName.endsWith( ".xls")) { workbook = new HSSFWorkbook(is); // Excel 2007/2010 } } } catch ( FileNotFoundException e) { e.printStackTrace(); } catch ( IOException e) { e.printStackTrace(); } finally { if (is!= null) { try { is.close(); } catch ( IOException e) { e.printStackTrace(); } } } } return workbook; }

继续阅读