导入依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>5.2.2</version>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.23</version>
</dependency>
创建util工具包以及Excel工具类
public class ExcelUtils {
public static void PraseExcel(InputStream inputStream){
ExcelUtil.readBySax(inputStream,0,(sheetIndex,rowIndex,list) ->{
System.out.println(sheetIndex);
System.out.println(rowIndex);
System.out.println(list);
});
}
}
搭建Controller层进行测试
@Controller
public class ExcelPraseController {
@RequestMapping("/ExcelParse")
@ResponseBody
public String pubggupload(@RequestParam("file")MultipartFile file, Model model) throws Exception{
String name=file.getOriginalFilename();
System.out.println(name);
if(!name.substring(name.length()-4).equals(".xls")){
return "格式不正确";
}
ExcelUtils.PraseExcel(file.getInputStream());
return "输出成功";
}
}
编写前端界面
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
<form method="post" enctype="multipart/form-data" action="http://localhost:8080/ExcelParse">
<input type="file" name="file" /><input type="submit" value="上传"/>
</form>
</body>
</html>
功能测试
后端返回数据文章来源:https://www.toymoban.com/news/detail-808909.html
文章来源地址https://www.toymoban.com/news/detail-808909.html
到了这里,关于springboot-简单测试 前端上传Excel表格后端解析数据的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!