导入依赖
<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")@ResponseBodypublic 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>
功能测试
后端返回数据