/** * @title * @description 创建指定的excel对象 * @author HUAZAI * @param * <ul> * <li></li> * <li></li> * <ul> * @return * <ul> * <li></li> * <li></li> * <ul> * @throws * @date 2018-03-14 11:51:23 */ public static Workbook workBookFactory(MultipartFile file) { //获得文件名 String fileName = file.getOriginalFilename(); //创建Workbook工作薄对象,表示整个excel Workbook workbook = null; try { //获取excel文件的io流 InputStream input = file.getInputStream(); //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象 if(fileName.endsWith(XLS)){ //2003 workbook = new HSSFWorkbook(input); }else if(fileName.endsWith(XLSX)){ //2007 workbook = new XSSFWorkbook(input); }
//其中if和else if可以使用
// WorkbookFactory.create(input);替换} catch (IOException e) { logger.error(e.getMessage()) ; } catch (InvalidFormatException e) { e.printStackTrace() ; } return workbook ; } /** * @title * @description 获取解析数据 * @author HUAZAI * @param * <ul> * <li> type 1 word 2url </li> * <li></li> * <ul> * @return * <ul> * <li></li> * <li></li> * <ul> * @throws * @date 2018-03-14 18:42:19 */ public static JSONObject readExcel(Workbook workbook , int type){ JSONObject json = new JSONObject() ; // 创建返回对象,把每行中的值作为一个数组,所有行作为一个集合返回 Multimap<String , String> treeMultimap = TreeMultimap. create() ; if(workbook != null) { for ( int sheetNum = 0 , sheetCount = workbook.getNumberOfSheets() ; sheetNum < sheetCount ; sheetNum++) { // 获得当前 sheet 工作表 Sheet sheet = workbook.getSheetAt(sheetNum) ; if (sheet == null) { continue; } // 获得当前 sheet 的开始行 int firstRowNum = sheet.getFirstRowNum() ; // 获得当前 sheet 的结束行 int lastRowNum = sheet.getLastRowNum() ; // 循环除了第一行的所有行 for ( int rowNum = firstRowNum + 1 ; rowNum <= lastRowNum ; rowNum++) { // 获得当前行 Row row = sheet.getRow(rowNum) ; if (row == null) { continue; } // 获得当前行的开始列 int firstCellNum = row.getFirstCellNum() ; // 获得当前行的列数 int cellCount = row.getPhysicalNumberOfCells() ; // 获取当前行的结束列 int lastCellNum = row.getLastCellNum() ; // 循环当前行 // for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){ // Cell cell = row.getCell(cellNum); // cells[cellNum] = getCellValue(cell); // } // 数据不合法 if (firstCellNum >= 1) { logger.info( " 数据不合法 文件内容格式错误 第 " + (rowNum+ 1) + " 行 ") ; continue; } Cell cell1 = row.getCell( 0) ; Cell cell2 = row.getCell( 1) ; if (cell1 == null) { logger.info( " 数据不合法 文件内容格式错误 没有数据 第 " + (rowNum+ 1) + " 行 ") ; continue; } cell1.setCellType(CellType. STRING) ; if (StringUtils. isBlank(cell1.getStringCellValue())) { logger.info( " 数据不合法 文件内容格式错误 没有数据 第 " + (rowNum+ 1) + " 行 ") ; continue; } if (cell1.getCellTypeEnum() == CellType. ERROR || cell2 != null && cell2.getCellTypeEnum() == CellType. ERROR ) { logger.info( " 丢弃数据 数据类型错误 key " + cell1 + " value " + cell2 + " 第 " + (rowNum+ 1) + " 行 ") ; continue; } if (type == 1) { treeMultimap.put( getCellValue(cell1) , StringUtils. trim( getCellValue(cell2))) ; } else if (type == 2) { treeMultimap.put(StringUtils. trim( getCellValue(cell1)) , StringUtils. trim( getCellValue(cell2))) ; } } } try { workbook.close() ; } catch (IOException e) { logger.error( " 关闭资源异常 " , e) ; } } // if (treeMultimap.isEmpty()){ // json.put("resultCode", 2); // return json; // } json.put( "resultCode" , 1) ; json.put( "resultData" , treeMultimap) ; return json ; } public static String getCellValue(Cell cell){ String cellValue = "" ; if(cell == null){ return cellValue ; } // 把数字当成 String 来读,避免出现 1 读成 1.0 的情况 if (cell.getCellTypeEnum() == CellType. NUMERIC) { cell.setCellType(CellType. STRING) ; } // 判断数据的类型 if (cell.getCellTypeEnum() == CellType. NUMERIC) { // 数字类型 cellValue = String. valueOf(cell.getNumericCellValue()) ; } else if (cell.getCellTypeEnum() == CellType. STRING) { // 字符串类型 cellValue = String. valueOf(cell.getStringCellValue()) ; } else if (cell.getCellTypeEnum() == CellType. BOOLEAN) { //Boolean cellValue = String. valueOf(cell.getBooleanCellValue()) ; } else if (cell.getCellTypeEnum() == CellType. FORMULA) { // 公式 cellValue = String. valueOf(cell.getCellFormula()) ; } else if (cell.getCellTypeEnum() == CellType. BLANK) { // 空值 cellValue = "" ; } else if (cell.getCellTypeEnum() == CellType. ERROR) { // 错误 cellValue = "error" ; } else { cellValue = "unkown" ; } return cellValue ; }
使用版本及jar包
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.17</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>3.17</version> </dependency>