当前位置:网站首页>项目实战 | 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、导出结果
边栏推荐
- 3D reconstruction open source code summary [keep updated]
- Ministry of transport and Ministry of Education: widely carry out water traffic safety publicity and drowning prevention safety reminders
- 交通运输部、教育部:广泛开展水上交通安全宣传和防溺水安全提醒
- Confusing basic concepts member variables local variables global variables
- Summary of "reversal" problem in challenge Programming Competition
- Nodemon installation and use
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- Multiple linear regression (sklearn method)
- Hi Fun Summer, play SQL planner with starrocks!
- Applet data attribute method
猜你喜欢

Kotlin introductory notes (II) a brief introduction to kotlin functions

Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)

一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势

Progressive JPEG pictures and related

Wxml template syntax

What is a firewall? Explanation of basic knowledge of firewall
![3D reconstruction open source code summary [keep updated]](/img/ec/984aede7ef9e758abd52fb5ff4e144.jpg)
3D reconstruction open source code summary [keep updated]

C form click event did not respond

Applet (global data sharing)

利用请求头开发多端应用
随机推荐
[beauty of algebra] solution method of linear equations ax=0
Applet global style configuration window
信息与熵,你想知道的都在这里了
【ManageEngine】如何利用好OpManager的报表功能
Array, date, string object method
Blogger article navigation (classified, real-time update, permanent top)
The combination of deep learning model and wet experiment is expected to be used for metabolic flux analysis
【愚公系列】2022年7月 Go教学课程 003-IDE的安装和基本使用
利用请求头开发多端应用
[code practice] [stereo matching series] Classic ad census: (6) multi step parallax optimization
c#比较两张图像的差异
Information and entropy, all you want to know is here
AUTOSAR从入门到精通100讲(103)-dbc文件的格式以及创建详解
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
2309. 兼具大小写的最好英文字母
Node collaboration and publishing
Return of missing persons
L'information et l'entropie, tout ce que vous voulez savoir est ici.
Solution to the problems of the 17th Zhejiang University City College Program Design Competition (synchronized competition)
我的一生.