我有一个带有弹簧启动的休息api工作正常它在临时文件中上传excel文件然后将它添加到数据库
然而,我是棱角分明Js的初学者,所以即使经过长时间的互联网搜索,我也无法做到这一点,我将不胜感激任何帮助
这是我的其余API的代码
@RestController
public class UploadController {
@Autowired
PosteDao posteDao ;
//Save the uploaded file to this folder
private static String UPLOADED_FOLDER = "C://Temp//";
@PostMapping("/doUploadFile") // //new annotation since 4.3
public String singleFileUpload(@RequestParam("file") MultipartFile file,
RedirectAttributes redirectAttributes) {
if (file.isEmpty()) {
redirectAttributes.addFlashAttribute("message", "Please select a file to upload");
return "redirect:uploadStatus";
}
try {
// Get the file and save it somewhere
byte[] bytes = file.getBytes();
Path path = Paths.get(UPLOADED_FOLDER + file.getOriginalFilename());
Files.write(path, bytes);
redirectAttributes.addFlashAttribute("message",
"You successfully uploaded '" + file.getOriginalFilename() + "'");
processExcel();
} catch (IOException e) {
e.printStackTrace();
}
return "greaat";
}
public String processExcel() throws IOException {
try {
InputStream ExcelFileToRead = new FileInputStream("C:\\\\list.xlsx");
//définir l'objet workbook
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
//Itérer sur le nombre de sheets
for(int i=0;i<wb.getNumberOfSheets();i++)
{
XSSFSheet sheet = wb.getSheetAt(i);
//Itérer sur les lignes
Row row;
for(int k=1;k<sheet.getLastRowNum()+1;k++)
{
row = (Row) sheet.getRow(k); //sheet number
int idPoste;
if( row.getCell(0)==null) { idPoste = 0; }
else idPoste= (int) row.getCell(0).getNumericCellValue();
String libelle;
if( row.getCell(1)==null) { libelle = "null";} //suppose excel cell is empty then its set to 0 the variable
else libelle = row.getCell(1).toString(); //else copies cell data to name variable
System.out.println(libelle);
System.out.println(idPoste);
}
}} catch (Exception e) {
System.err.println("Erreur");
// TODO Auto-generated catch block
e.printStackTrace();
}
return "data extracted successfully";
}
}