@Override public ResponseEntity<? extends Response> acceptExcelFileAndSenderName(MultipartFile filePath, String senderName) throws IOException { String fileName = filePath.getOriginalFilename(); if (fileName.substring(fileName.length() - 5).equals(".xlsx")) { try (InputStream excelFile = filePath.getInputStream()) { String phoneNumber = ""; String textMessage = ""; Workbook workbook = new XSSFWorkbook(excelFile); Sheet datatypeSheet = workbook.getSheetAt(0); Iterator<Row> iterator = datatypeSheet.iterator(); while (iterator.hasNext()) { Row currentRow = iterator.next(); Iterator<Cell> cellIterator = currentRow.iterator(); while (cellIterator.hasNext()) { Cell currentCell = cellIterator.next(); if (currentCell.getCellType() == CellType.NUMERIC) { phoneNumber = NumberToTextConverter.toText(currentCell.getNumericCellValue()); } else if (currentCell.getCellType() == CellType.STRING) { textMessage = String.valueOf(currentCell.getStringCellValue()); } } FileDetail fileDetail = new FileDetail(); fileDetail.setPhoneNumber(phoneNumber); fileDetail.setTextMessage(textMessage); fileDetail.setSender(senderName); this.fileDetailRepository.save(fileDetail); } excelFile.close(); String destination = "C:\\Users\\anar.memmedov\\Desktop\\app\\" + filePath.getOriginalFilename(); File file1 = new File(destination); filePath.transferTo(file1); return new ResponseEntity<>(new SuccessResponse(MessageCase.FILE_SUCCESSFULLY_WRITTEN_TO_DATABASE.getMessage(), 200), HttpStatus.OK); } catch (IOException e) { e.printStackTrace(); } } return new ResponseEntity<>(new ErrorResponse(MessageCase.FAILED_HAPPEND_WHEN_FILE_WRITTEN_TO_DATABASE.getMessage(), 400), HttpStatus.BAD_REQUEST); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)