public Result doExcel(MultipartFile file) { InputStream inputStream = null; XSSFWorkbook wb = null; Map<String,String> map = new HashMap<>(); try { List<QymAppealSummary> list = new ArrayList<>(); inputStream = file.getInputStream(); wb = new XSSFWorkbook(inputStream); Sheet sheet = wb.getSheetAt(0); for (int rowIndex = 1; rowIndex <= sheet.getLastRowNum(); rowIndex++) { Row row = sheet.getRow(rowIndex); if (row == null) { continue; } QymAppealSummary summary = new QymAppealSummary(); summary.setAppealCode(IdUtil.getId()); summary.setEntName(PoiUtil.getCellValueTwo(row.getCell(3))); summary.setAppealTime(DateAddUtil. parse(PoiUtil.getCellValueTwo(row.getCell(1)),DateAddUtil.DATE_FORMAT_1)); list.add(summary); //记录唯一的标识和内容 map.put(summary.getAppealCode(),PoiUtil.getCellValueTwo(row.getCell(9))); } qymAppealSummaryService.saveBatch(list); iRedisOperateDAO.set("excel_appeal_map",JSON.toJSONString(map),60*60); } catch (Exception e) { logger.error("",e); return Result.fail("导入失败"); } finally { try { inputStream.close(); wb.close(); } catch (IOException e) { e.printStackTrace(); } } return Result.success(); }
//处理单元格格式
public static String getCellValueTwo(Cell cell) { String cellValue = ""; if (cell == null) { return cellValue; } // 判断数据的类型 switch (cell.getCellType()) { case Cell.CELL_TYPE_NUMERIC: // 数字 short format = cell.getCellStyle().getDataFormat(); SimpleDateFormat sdf = null; if(format == 14 || format == 31 || format == 57 || format == 58){ //日期 sdf = new SimpleDateFormat("yyyy-MM-dd"); }else if (format == 20 || format == 32) { //时间 sdf = new SimpleDateFormat("HH:mm"); }else if (HSSFDateUtil.isCellDateFormatted(cell)) { //先注释日期类型的转换,在实际测试中发现HSSFDateUtil.isCellDateFormatted(cell)只识别2014/02/02这种格式。 // 如果是Date类型则,取得该Cell的Date值 // 对2014-02-02格式识别不出是日期格式 Date date = cell.getDateCellValue(); DateFormat formater = new SimpleDateFormat(DateAddUtil.DATE_FORMAT_1); cellValue= formater.format(date); } else { // 如果是纯数字 // 取得当前Cell的数值 cellValue = NumberToTextConverter.toText(cell.getNumericCellValue()); } break; case Cell.CELL_TYPE_STRING: // 字符串 cellValue = String.valueOf(cell.getStringCellValue()); break; case Cell.CELL_TYPE_BOOLEAN: // Boolean cellValue = String.valueOf(cell.getBooleanCellValue()); break; case Cell.CELL_TYPE_FORMULA: // 公式 cellValue = String.valueOf(cell.getCellFormula()); break; case Cell.CELL_TYPE_BLANK: // 空值 cellValue = null; break; case Cell.CELL_TYPE_ERROR: // 故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } return cellValue; }