public static void checkFile(MultipartFile file) throws IOException{ //判断文件是否存在 if(null == file){ throw new AudaqueException("文件不存在!"); } //获得文件名 String fileName = file.getOriginalFilename(); //判断文件是否是excel文件 if(!fileName.endsWith(xls) && !fileName.endsWith(xlsx)){ throw new AudaqueException(fileName + "不是excel文件"); } } public static Workbook getWorkBook(MultipartFile file) { //获得文件名 String fileName = file.getOriginalFilename(); //创建Workbook工作薄对象,表示整个excel Workbook workbook = null; try { //获取excel文件的io流 InputStream is = file.getInputStream(); //根据文件后缀名不同(xls和xlsx)获得不同的Workbook实现类对象 if(fileName.endsWith(xls)){ //2003 workbook = new HSSFWorkbook(is); }else if(fileName.endsWith(xlsx)){ //2007 workbook = new XSSFWorkbook(is); } } catch (IOException e) { log.info(e.getMessage()); } return workbook; } public static String getCellValue(Cell cell){ String cellValue = ""; if(cell == null){ return cellValue; } //把数字当成String来读,避免出现1读成1.0的情况 if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){ cell.setCellType(Cell.CELL_TYPE_STRING); } //判断数据的类型 switch (cell.getCellType()){ case Cell.CELL_TYPE_NUMERIC: //数字 cellValue = String.valueOf(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 = ""; break; case Cell.CELL_TYPE_ERROR: //故障 cellValue = "非法字符"; break; default: cellValue = "未知类型"; break; } return cellValue; }
/** * 读入excel文件,解析后返回 * @param file * @throws IOException */ public List<AdqmMetadata> readExcel(MultipartFile file,List<AdqmMetadata> dataList) throws IOException, AudaqueException{ //检查文件 checkFile(file); //获得Workbook工作薄对象 Workbook workbook = getWorkBook(file); //dataList 创建返回对象,把每行中的值作为一个对象,所有行作为一个集合返回 if(workbook != null){ for(int sheetNum = 0;sheetNum < workbook.getNumberOfSheets();sheetNum++){ String sheetName = workbook.getSheetAt(sheetNum).getSheetName(); // 根据name判断sheet类型 if (sheetName.startsWith("代码清单")) { Sheet sheet = workbook.getSheetAt(sheetNum); if(sheet == null){ continue; } //获得当前sheet的开始行 int firstRowNum = sheet.getFirstRowNum(); //获得当前sheet的结束行 int lastRowNum = sheet.getLastRowNum(); //循环除了第一行的所有行 // 代码集数据 // 获取列标题 Row row0 = sheet.getRow(firstRowNum); // 如果列名不对 终止导入 暴露错误 for(int cellNum = 0; cellNum < 5;cellNum++){ Cell cell = row0.getCell(cellNum); if (!"代码集编码".equals(getCellValue(cell))&&cellNum==0) { throw new AudaqueException(sheetName + "表单第1列标题模板错误"); }else if (!"标准编号".equals(getCellValue(cell))&&cellNum==1) { throw new AudaqueException(sheetName + "表单第2列标题模板错误"); }else if (!"代码集名称".equals(getCellValue(cell))&&cellNum==2) { throw new AudaqueException(sheetName + "表单第3列标题模板错误"); }else if (!"标准类型".equals(getCellValue(cell))&&cellNum==3) { throw new AudaqueException(sheetName + "表单第4列标题模板错误"); }else if (!"标准代码".equals(getCellValue(cell))&&cellNum==4) { throw new AudaqueException(sheetName + "表单第5列标题模板错误"); }else if (!"代码名称".equals(getCellValue(cell))&&cellNum==5) { throw new AudaqueException(sheetName + "表单第6列标题模板错误"); } } for(int rowNum = firstRowNum+1;rowNum <= lastRowNum;rowNum++){ //获得当前行 Row row = sheet.getRow(rowNum); if(row == null){ continue; } if (row != null ){ //获得当前行的开始列 int firstCellNum = row.getFirstCellNum(); //获得当前行的列数 int lastCellNum = row.getPhysicalNumberOfCells(); String[] cells = new String[row.getPhysicalNumberOfCells()]; //循环当前行 AdqmMetadata adqmMetadata = new AdqmMetadata(); for(int cellNum = firstCellNum; cellNum < lastCellNum;cellNum++){ Cell cell = row.getCell(cellNum); //代码集编码 String valus=getCellValue(cell); if (cellNum==0){ if (StringUtils.isNotBlank(valus)){ adqmMetadata.setCodesetCode(valus); }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第1列代码集编码内容不能为空!"); } }else if (cellNum==1){ // 代码集描述 adqmMetadata.setValusDesc(valus); }else if (cellNum==2){ // 代码集描述 if (StringUtils.isNotBlank(valus)){ adqmMetadata.setRemarks(valus); }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第3列代码集名称内容不能为空!"); } }else if (cellNum==3){ // 标准类型 if (StringUtils.isNotBlank(valus)){ DateTypeCodeEnum dateTypeCodeEnum=DateTypeCodeEnum.getEnumByValuesName(valus); if (dateTypeCodeEnum!=null){ adqmMetadata.setDataType(dateTypeCodeEnum.getValue()); }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第4列的标准类型不是标准类型"); } }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第4列标准类型内容为空"); } }else if (cellNum==4){ // 代码 if (StringUtils.isNotBlank(valus)){ adqmMetadata.setMetadataCode(valus); }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第5列标准代码内容不能为空!"); } }else if (cellNum==5){ // 名称 if (StringUtils.isNotBlank(valus)){ adqmMetadata.setMetadataName(valus); }else{ throw new AudaqueException(sheetName + "表单第"+rowNum+1+"行第6列代码名称内容不能为空!"); } } } // 添加到集合中 adqmMetadata.setCreateUser(getUserId()); adqmMetadata.setCreateTime(new Date()); dataList.add(adqmMetadata); }else{ } } } workbook.close(); } } return dataList; }
@RequestMapping(value = "importAdqmMetadataExcel1.do") @ResourcePermissions(name = "批量导入",value = "importAdqmMetadataExcel1") @ResponseBody public Map<String,Object> importAdqmMetadataExcel1(HttpServletRequest request, HttpServletResponse response)throws IOException, AudaqueException { Map<String,Object> result = new HashMap<String, Object>(); int effectives = 0; MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest)request; Map<String, MultipartFile> files = multipartRequest.getFileMap(); List<AdqmMetadata> dataList = new ArrayList<AdqmMetadata>(); for(Map.Entry<String, MultipartFile> entry :files.entrySet()) { MultipartFile file = entry.getValue(); //检查文件 dataList=readExcel(file,dataList); } if (dataList!=null&&dataList.size()>0){ adqmMetadataService.batchInsertAdqmMetadataSelective(dataList); result.put("effectives", dataList.size()); }else{ result.put("effectives", effectives); } return result; }