Untitled
unknown
plain_text
3 years ago
1.4 kB
6
Indexable
@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()));
}
}
}
Editor is loading...