spring boot 接收jquery ajax上传的excel文件并处理后返回给前端页面

前端页面:




    
    test
    



    

spring boot 后端的controller:

@PostMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file) throws IOException, InvalidFormatException {
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String dir=System.getProperty("user.dir");
        String destFileName=dir+File.separator +"uploadedfiles_"+fileName;
        System.out.println(destFileName);
        File destFile = new File(destFileName);
        file.transferTo(destFile);

        System.out.println("上传成功");
        System.out.println("开始读取EXCEL内容");

        Sheet sheet;
        InputStream fis = null;

        fis = new FileInputStream(destFileName);

        Workbook workbook = null;
        try {
            workbook= new XSSFWorkbook(destFile);
        } catch (Exception ex) {
            workbook = new HSSFWorkbook(fis);
        }
        sheet = workbook.getSheetAt(0);

        int totalRowNum = sheet.getLastRowNum();

        System.out.println("当前表格共有:"+totalRowNum+"行");

        String tableString="";
        tableString+="";
        for(int i=0;i";
                    System.out.println(cellValue);
                }

            }
            tableString+="";
        }
        tableString+="
"; return "上传成功"+tableString; }

 

你可能感兴趣的:(java)