1、创建excel模板
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/1a4a3d9c2d2f8561a207af2c73fa99b4.png)
- 注:如果要多个sheet循环使用第一个sheet模板的话,最下方sheet只能有一个
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/0b9ccc2c4dcf3164618b24c3667f57e8.png)
2、引入excel依赖
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.2</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-base</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-web</artifactId>
<version>4.4.0</version>
</dependency>
<dependency>
<groupId>cn.afterturn</groupId>
<artifactId>easypoi-annotation</artifactId>
<version>4.4.0</version>
</dependency>
3、封装数据
import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.TemplateExportParams;
import org.apache.poi.ss.usermodel.Workbook;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
@GetMapping("/exportTemplateMore")
public void exportTemplateMore(HttpServletResponse response){
Map<String,String> info = new HashMap<>();
info.put("title","人员数据");
Map<String,String> vos0 = new HashMap<>();
vos0.put("name","张三");
vos0.put("age","26");
Map<String,String> vos1 = new HashMap<>();
vos1.put("name","李四");
vos1.put("age","25");
List<Map<String,String>> volist = new ArrayList<>();
volist.add(vos0);
volist.add(vos1);
Map<Integer, List<Map<String, Object>>> map=new HashMap<>();
for (int i = 0; i < 10; i++) {
List<Map<String, Object>> exportList=new ArrayList<>();
Map<String,Object> m1=new HashMap<>();
m1.put("info",info);
m1.put("sheetName",(i+1)+"号");
m1.put("list", volist);
exportList.add(m1);
map.put(i,exportList);
}
try {
String path = "D:\\excel\\template\\多sheet模板导出.xlsx";
TemplateExportParams params = new TemplateExportParams(path);
Workbook workbook = ExcelExportUtil.exportExcelClone(map, params);
response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment;filename=%s", URLEncoder.encode( "多模板导出测试.xlsx", "UTF-8")));
OutputStream os = new BufferedOutputStream(response.getOutputStream());
workbook.write(os);
os.flush();
os.close();
} catch (Exception e) {
e.printStackTrace();
}
}
4、测试验证
- 1)使用postman请求下载excel
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3c7c8601caf0e9444187b7975140aefa.png)
- 2)验证结果
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/3e83d90b3d5168ded059cf691ecf5638.png)