SpringBoot導入excle文件數據

本文主要描述,Springboot框架下上傳excel,處理裏面相關數據作邏輯分析,因爲用到的是先後端分離技術,這裏記錄的主要是後端java部分,經過與前端接口進行對接實現功能前端

1.在pom.xml文件中導入註解,主要利用POIjava

<dependency>
      <groupId>org.apache.poi</groupId>
      <artifactId>poi-ooxml</artifactId>
      <version>3.9</version>
</dependency>
<dependency>
      <groupId>commons-fileupload</groupId>
      <artifactId>commons-fileupload</artifactId>
      <version>1.3.1</version>
</dependency>
<dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.4</version>
</dependency>

2. springboot java實現代碼spring

@RequestMapping(value="/uploadTest", method = RequestMethod.POST)
    public String uploadTest(@RequestParam MultipartFile file, HttpServletRequest request){

        try {
            if(file==null)
                return BaseCode.retCode(1, "上傳文件不能爲空").toString();
            String fileName = file.getOriginalFilename();
            if (!fileName.matches("^.+\\.(?i)(xls)$") && !fileName.matches("^.+\\.(?i)(xlsx)$")) {
                return BaseCode.retCode(1, "上傳文件格式錯誤,請上傳後綴爲.xls或.xlsx的文件").toString();
            }

            boolean isExcel2003 = true;
            if (fileName.matches("^.+\\.(?i)(xlsx)$")) {
                isExcel2003 = false;
            }
            InputStream is = file.getInputStream();
            Workbook wb = null;
            if (isExcel2003) {
                wb = new HSSFWorkbook(is);
            } else {
                wb = new XSSFWorkbook(is);
            }
            Sheet sheet = wb.getSheetAt(0);
            if(sheet!=null){
                //notNull = true;
            }
            for (int r = 1; r <= sheet.getLastRowNum(); r++) {
                Row row = sheet.getRow(r);
                if (row == null) {
                    continue;
                }
               
    System.out.println(row.getCell(0).getStringCellValue());
 } 
}
catch (IOException e) {
}
return BaseCode.retCode(ResultCode.success).toString();
}

3. PostMan調用方式:apache

 

 完畢!後端

相關文章
相關標籤/搜索