当前位置:网站首页>项目实战 | 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、导出结果
边栏推荐
- Svg optimization by svgo
- C # compare the differences between the two images
- Codeworks round 681 (Div. 2) supplement
- 【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
- Wxml template syntax
- Shutter uses overlay to realize global pop-up
- Attention is all you need
- [code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
- 阿里十年测试带你走进APP测试的世界
- Hi Fun Summer, play SQL planner with starrocks!
猜你喜欢
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
Wxss template syntax
Understanding rotation matrix R from the perspective of base transformation
什么是防火墙?防火墙基础知识讲解
Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
图神经网络+对比学习,下一步去哪?
Applet network data request
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)
Codeworks round 639 (Div. 2) cute new problem solution
随机推荐
Using request headers to develop multi terminal applications
Confusing basic concepts member variables local variables global variables
Codeworks round 639 (Div. 2) cute new problem solution
Applet (subcontracting)
交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
Information and entropy, all you want to know is here
Ecmascript6 introduction and environment construction
[beauty of algebra] solution method of linear equations ax=0
Chris LATTNER, the father of llvm: why should we rebuild AI infrastructure software
Codeforces Round #648 (Div. 2) D. Solve The Maze
Node collaboration and publishing
Multiple linear regression (sklearn method)
STM32简易多级菜单(数组查表法)
Introduction Guide to stereo vision (1): coordinate system and camera parameters
[code practice] [stereo matching series] Classic ad census: (4) cross domain cost aggregation
OpenGL - Coordinate Systems
编辑器-vi、vim的使用
2311. Longest binary subsequence less than or equal to K
Wechat H5 official account to get openid climbing account
信息与熵,你想知道的都在这里了