1. 程式人生 > >SpringBoot導入excle文件數據

SpringBoot導入excle文件數據

圖片 original 導入 lean turn http mapping () cep

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

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

<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實現代碼

@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調用方式:

技術分享圖片

完畢!

SpringBoot導入excle文件數據