SpringBoot之excel表格导出
忙里偷闲,最近一直在写excel表的导出,导的挺费劲的,一开始没有理解,慢慢的懂了点儿,整理出来,方便下次复用。能够提供给别人一个参考那就更好了
- 第一步:导入poi的jar包
<!--poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.1</version>
<exclusions>
<exclusion>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.xmlbeans</groupId>
<artifactId>xmlbeans</artifactId>
<version>3.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.1</version>
</dependency>
- 第二步:controller,在controller中将标题List写上,把表头对应的数据库的字段List写上,这个字段要和数据库中的字段一一对应,这样数据库中查询出的数据便可以一一对应放入导单元格中了
@ApiOperation(value = "导出excel")
@GetMapping("/exportExcel")
@ApiImplicitParam(name = "title", value = "标题", paramType = "query", dataType = "String",required = true)
public ResponseMessage exportExcel(HttpServletResponse response, HttpServletRequest request, String title
) {
// 这个是表头list
List<String> titleList = new ArrayList<>