1.准備excel模板

注: 數據類型為map用{},數據類型為List用{.}
2.在項目中resource目錄下新建目錄static,然后把准備好的模板扔進去
3.maven中添加相關依賴
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.1.6</version>
<exclusions>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
</exclusion>
<exclusion>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
</exclusion>
</exclusions>
</dependency>
4.導出excel並返回文件名稱供前端下載
public AjaxResult test(Map map,List list) throws IOException {
InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("static/"+"test.xlsx");
String fileName = UUID.randomUUID().toString() + "_" + "test.xlsx";
String downloadPath = RuoYiConfig.getDownloadPath() + fileName;
OutputStream out=new FileOutputStream(downloadPath);
ExcelWriter excelWriter = EasyExcel.write(out).withTemplate(inputStream).build();
WriteSheet writeSheet = EasyExcel.writerSheet().build();
excelWriter.fill(list, writeSheet);
excelWriter.fill(map, writeSheet);
excelWriter.finish();
return AjaxResult.success(fileName);
}
5.結果:

