Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.4 kB
1
Indexable
Never
@Service
public class QuestionService {

    @Autowired
    private QuestionRepository questionRepository;

    public void addQuestionsFromExcel(InputStream inputStream) throws IOException {
        Workbook workbook = new HSSFWorkbook(inputStream);
        Sheet sheet = workbook.getSheetAt(0);

        Iterator<Row> rowIterator = sheet.iterator();

        while (rowIterator.hasNext()) {
            Row row = rowIterator.next();

            Question question = new Question();

            Cell cell = row.getCell(0);
            question.setQuestionText(cell.getStringCellValue());

            cell = row.getCell(1);
            question.setOption1(cell.getStringCellValue());

            cell = row.getCell(2);
            question.setOption2(cell.getStringCellValue());

            cell = row.getCell(3);
            question.setOption3(cell.getStringCellValue());

            cell = row.getCell(4);
            question.setOption4(cell.getStringCellValue());

            cell = row.getCell(5);
            question.setCorrectAnswer(cell.getStringCellValue());

            questionRepository.save(question);
        }
    }

    public void deleteQuestionsFile(String fileName) throws IOException {
        Resource resource = new ClassPathResource("static/files/" + fileName);

        if (resource.exists()) {
            Files.delete(Paths.get(resource.getURI()));
        }
    }
}