Untitled
unknown
plain_text
a year ago
1.6 kB
1
Indexable
Never
public void addQuestionsFromExcel(MultipartFile file) throws InvalidFileException { try { Workbook workbook = WorkbookFactory.create(file.getInputStream()); Sheet sheet = workbook.getSheetAt(0); // Validate the file format if (!isValidExcelFile(sheet)) { throw new InvalidFileException("Invalid file format"); } // Parse the questions from the Excel file List<Question> questions = new ArrayList<>(); Iterator<Row> rowIterator = sheet.rowIterator(); while (rowIterator.hasNext()) { Row row = rowIterator.next(); if (row.getRowNum() == 0) { continue; // Skip the header row } String questionText = row.getCell(0).getStringCellValue(); String option1 = row.getCell(1).getStringCellValue(); String option2 = row.getCell(2).getStringCellValue(); String option3 = row.getCell(3).getStringCellValue(); String option4 = row.getCell(4).getStringCellValue(); int correctOption = (int) row.getCell(5).getNumericCellValue(); int level = (int) row.getCell(6).getNumericCellValue(); Question question = new Question(questionText, option1, option2, option3, option4, correctOption, level); questions.add(question); } // Save the questions to the database questionRepository.saveAll(questions); } catch (IOException | InvalidFormatException e) { throw new InvalidFileException("Invalid file format"); } }