SpringBoot导入excle文件数据

本文主要描述,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文件数据

完毕!

上一篇:了解JavaScript 面向对象基础 & 原型与对象


下一篇:C#生成和识别二维码