try {this.upload(file);String originName = file.getOriginalFilename();// 取出文件的后缀int count = 0;for (int i = 0; i < originName.length(); i++) {if (originName.charAt(i) == '.') {count = i;break;}}String fileType = originName.substring(count + 1); //文件类型if(!fileType.equals("xls")&&!fileType.equals("xlsx")){return MsgOut.fail("只支持xls、xlsx类型文件");}SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String format = sdf.format(new Date());String savePath = uploadpath + "\\" + "files" + "\\" + fileType + "\\" + format;String fielPath=savePath + "\\" + originName;//文档File file1=new File(fielPath);FileInputStream inputStream = new FileInputStream(file1);//创建工作本,参数为工作流Workbook workbook=null;if(fileType.equals("xls")) {workbook = new HSSFWorkbook(inputStream);}else if(fileType.equals("xlsx")){workbook = new XSSFWorkbook(inputStream);}if(workbook!=null) {// 获取第一个表单SheetSheet sheetAt = workbook.getSheetAt(0);String sheetName = sheetAt.getSheetName();//默认第一行为标题行,i = 0//XSSFRow titleRow = sheetAt.getRow(0);// 循环获取每一行数据ThinkUserSpecialRank thinkUserSpecialRank = new ThinkUserSpecialRank();for (int i = 0; i < sheetAt.getPhysicalNumberOfRows(); i++) {Row row = sheetAt.getRow(i);// 读取每一格内容for (int index = 0; index < row.getPhysicalNumberOfCells(); index++) {Cell cell = row.getCell(index);cell.setCellType(Cell.CELL_TYPE_STRING);if (cell.getStringCellValue().equals("")) {continue;}
}thinkUserSpecialRank.setSpecialName(sheetName);this.thinkUserSpecialRankMapper.insert(thinkUserSpecialRank);}}} catch (IOException e) {e.printStackTrace();return MsgOut.OK("导入失败");}
private void upload(MultipartFile file) {// 获取文件原本的名字String originName = file.getOriginalFilename();// 判断文件类型Set<String> set = new HashSet<>();set.add(".xls");set.add(".xlsx");// 取出文件的后缀int count = 0;for (int i = 0; i < originName.length(); i++) {if (originName.charAt(i) == '.') {count = i;break;}}String endName = originName.substring(count); //取出文件类型String fileType = originName.substring(count + 1); //文件类型// 创建保存路径//日期格式SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");String format = sdf.format(new Date());String savePath = uploadpath + "\\" + "files" + "\\" + fileType + "\\" + format;// 保存文件的文件夹File folder = new File(savePath);// 判断路径是否存在,不存在则自动创建if (!folder.exists()) {folder.mkdirs();}String saveName = originName;try {File file1 = new File(folder, saveName);file.transferTo(file1);String filePath = savePath + "\\" + saveName;} catch (IOException e) {}}