当前位置:网站首页>项目实战 | Excel导出功能
项目实战 | Excel导出功能
2022-07-05 09:16:00 【Going_man】
项目实战 | Excel导出功能
一、Excel模板填充导出
场景:将每个教师的教案数据都导出到各个以教师命名的sheet
难点:怎么根据教师数据,生成多个以教师名命名sheet的Excel模板去填充?
解决方式:
1、生成Excel模板的输入IO流:
①pom.xml配置
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.17</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.17</version>
</dependency>
②通过基础Excel模板,动态生成多个sheet的Excel模板流
基础Excel模板:

动态生成多个sheet的excel模板流的代码:
private InputStream getTemplateIn(String fileName,List<TeachPlanList> teachPlanLists) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
//原模板只有一个sheet,通过poi复制出需要的sheet个数的模板
XSSFWorkbook workbook = new XSSFWorkbook(new ClassPathResource(fileName).getInputStream());
//设置模板的第一个sheet的名称
String sheet =teachPlanLists.get(0).getUserName()+"-"+ teachPlanLists.get(0).getUserId();
workbook.setSheetName(0,sheet);
for (int i = 1; i < teachPlanLists.size(); i++) {
//复制模板,得到第i个sheet
workbook.cloneSheet(0,teachPlanLists.get(i).getUserName()+"-"+teachPlanLists.get(i).getUserId());
}
//写到流里
workbook.write(bos);
byte[] bArray = bos.toByteArray();
return new ByteArrayInputStream(bArray);
}
2、将生成的Excel输入流当做模板,进行数据填充,导出Excel
public void exportTeachingPlanList(String schoolID, HttpServletResponse response) throws IOException {
List<TeachPlanList> teachPlanLists =teachPlanMgrBaseInfoMapper.selectTeachPlanBySchoolId(schoolID);
String fileName = URLEncoder.encode("个人教案库统计数据", "UTF-8");
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xls");
InputStream is = getTemplateIn("excel/TeachPlanData.xlsx",teachPlanLists);
ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream())
.withTemplate(is)
.registerConverter(new FileSizeConverter())
.build();
for (int i = 0; i < teachPlanLists.size(); i++) {
WriteSheet writeSheet = EasyExcel.writerSheet(i).build();
excelWriter.fill(teachPlanLists.get(i).getTeachPlanExcelInfoList(), writeSheet);
//填充单独统计数据
Map<String, Object> map = new HashMap<>(2);
map.put("userName", teachPlanLists.get(i).getUserName());
map.put("countTeachPlan", teachPlanLists.get(i).getCountTeachPlan());
//填充列表数据
excelWriter.fill(map, writeSheet);
}
excelWriter.finish();
}
3、导出结果展示:

二、由对象写成Excel
场景:将数据进行统计导出成文档
1、构造Excel导出对象
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShareTpExcelInfo {
@ExcelProperty("共享者名称")
@ColumnWidth(15)
private String sharerName;
@ExcelProperty("分享教案个数")
@ColumnWidth(15)
private Integer sharerCount;
@ExcelProperty("分享教案被使用次数")
@ColumnWidth(15)
private Integer useCount;
@ExcelProperty("分享教案平均被使用次数")
@ColumnWidth(15)
@JSONField(name="AvgUseCount")
private Double avgUseCount;
}
2、导出成Excel
public void exportShareTpExcel(HttpServletResponse response, List<ShareTpExcelInfo> list) throws IOException {
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
String fileName = URLEncoder.encode("共享教案库数据" + System.currentTimeMillis(), String.valueOf(StandardCharsets.UTF_8));
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), ShareTpExcelInfo.class).sheet("教案数据").doWrite(list);
}
3、导出结果
边栏推荐
- Creation and reference of applet
- Illustrated network: what is gateway load balancing protocol GLBP?
- 深入浅出PyTorch中的nn.CrossEntropyLoss
- 2309. The best English letters with both upper and lower case
- [ctfhub] Title cookie:hello guest only admin can get flag. (cookie spoofing, authentication, forgery)
- Ecmascript6 introduction and environment construction
- Newton iterative method (solving nonlinear equations)
- Meta tag details
- Uni app implements global variables
- 2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
猜你喜欢

Blogger article navigation (classified, real-time update, permanent top)

c语言指针深入理解

Composition of applet code

牛顿迭代法(解非线性方程)

What is a firewall? Explanation of basic knowledge of firewall
![Rebuild my 3D world [open source] [serialization-1]](/img/74/b6253845b43bc18f425d57695fba7c.jpg)
Rebuild my 3D world [open source] [serialization-1]

优先级队列(堆)

Hi Fun Summer, play SQL planner with starrocks!

22-07-04 西安 尚好房-项目经验总结(01)

Wxml template syntax
随机推荐
22-07-04 西安 尚好房-项目经验总结(01)
【阅读笔记】图对比学习 GNN+CL
Jenkins pipeline method (function) definition and call
【PyTorch Bug】RuntimeError: Boolean value of Tensor with more than one value is ambiguous
Codeforces round 684 (Div. 2) e - green shopping (line segment tree)
2020 "Lenovo Cup" National College programming online Invitational Competition and the third Shanghai University of technology programming competition
什么是防火墙?防火墙基础知识讲解
Golang foundation -- map, array and slice store different types of data
c#比较两张图像的差异
Attention is all you need
.NET服务治理之限流中间件-FireflySoft.RateLimit
Confusion matrix
OpenGL - Coordinate Systems
Alibaba cloud sends SMS verification code
scipy. misc. imread()
NIPS2021 | 超越GraphCL,GNN+对比学习的节点分类新SOTA
Introduction Guide to stereo vision (7): stereo matching
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
利用请求头开发多端应用
Kotlin introductory notes (I) kotlin variables and non variables