当前位置:网站首页>Project practice | excel export function
Project practice | excel export function
2022-07-05 09:26:00 【Going_ man】
Project practice | Excel Export function
Official documents : About Easyexcel | Easy Excel (alibaba.com)
One 、Excel Template fill export
scene : Export the teaching plan data of each teacher to each teacher named sheet
difficulty : According to the teacher data , Generate multiple named after teachers sheet Of Excel Template to fill ?
Solution :
1、 Generate Excel Template input IO flow :
①pom.xml To configure
<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>
② Pass the foundation Excel Templates , Generate multiple... Dynamically sheet Of Excel Template flow
Basics Excel Templates :

Generate multiple... Dynamically sheet Of excel Code of template flow :
private InputStream getTemplateIn(String fileName,List<TeachPlanList> teachPlanLists) throws IOException {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// There is only one original template sheet, adopt poi Copy what you need sheet Number of templates
XSSFWorkbook workbook = new XSSFWorkbook(new ClassPathResource(fileName).getInputStream());
// Set the first of the template sheet The name of
String sheet =teachPlanLists.get(0).getUserName()+"-"+ teachPlanLists.get(0).getUserId();
workbook.setSheetName(0,sheet);
for (int i = 1; i < teachPlanLists.size(); i++) {
// Copy template , Get the first i individual sheet
workbook.cloneSheet(0,teachPlanLists.get(i).getUserName()+"-"+teachPlanLists.get(i).getUserId());
}
// Write into the stream
workbook.write(bos);
byte[] bArray = bos.toByteArray();
return new ByteArrayInputStream(bArray);
}
2、 The generated Excel Input stream as template , Data filling , export Excel
public void exportTeachingPlanList(String schoolID, HttpServletResponse response) throws IOException {
List<TeachPlanList> teachPlanLists =teachPlanMgrBaseInfoMapper.selectTeachPlanBySchoolId(schoolID);
String fileName = URLEncoder.encode(" Statistics of personal teaching plan database ", "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);
// Fill in individual statistics
Map<String, Object> map = new HashMap<>(2);
map.put("userName", teachPlanLists.get(i).getUserName());
map.put("countTeachPlan", teachPlanLists.get(i).getCountTeachPlan());
// Fill in the list data
excelWriter.fill(map, writeSheet);
}
excelWriter.finish();
}
3、 Export results display :

Two 、 Written by objects Excel
scene : Statistics and export data into documents
1、 structure Excel Export object
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ShareTpExcelInfo {
@ExcelProperty(" Sharer name ")
@ColumnWidth(15)
private String sharerName;
@ExcelProperty(" Number of lesson plans shared ")
@ColumnWidth(15)
private Integer sharerCount;
@ExcelProperty(" The number of times shared lesson plans have been used ")
@ColumnWidth(15)
private Integer useCount;
@ExcelProperty(" The average number of times shared lesson plans are used ")
@ColumnWidth(15)
@JSONField(name="AvgUseCount")
private Double avgUseCount;
}
2、 Export to 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(" Share teaching plan database data " + System.currentTimeMillis(), String.valueOf(StandardCharsets.UTF_8));
response.setHeader("Content-Disposition", "attachment;filename=" + fileName + ".xlsx");
EasyExcel.write(response.getOutputStream(), ShareTpExcelInfo.class).sheet(" Lesson plan data ").doWrite(list);
}
3、 The results derived 
边栏推荐
- 一文详解图对比学习(GNN+CL)的一般流程和最新研究趋势
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- Introduction Guide to stereo vision (2): key matrix (essential matrix, basic matrix, homography matrix)
- Multiple solutions to one problem, asp Net core application startup initialization n schemes [Part 1]
- LeetCode 556. 下一个更大元素 III
- 编辑器-vi、vim的使用
- fs. Path module
- 2310. 个位数字为 K 的整数之和
- L'information et l'entropie, tout ce que vous voulez savoir est ici.
猜你喜欢

Progressive JPEG pictures and related

信息與熵,你想知道的都在這裏了
![[ManageEngine] how to make good use of the report function of OpManager](/img/15/dc15e638ae86d6cf1d5b989fe56611.jpg)
[ManageEngine] how to make good use of the report function of OpManager

高性能Spark_transformation性能

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

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

LeetCode 503. 下一个更大元素 II

Hosting environment API

c语言指针深入理解

Kotlin introductory notes (II) a brief introduction to kotlin functions
随机推荐
Confusion matrix
Introduction Guide to stereo vision (6): level constraints and polar correction of fusiello method
干货整理!ERP在制造业的发展趋势如何,看这一篇就够了
Kotlin introductory notes (VII) data class and singleton class
Wxss template syntax
22-07-04 西安 尚好房-项目经验总结(01)
生成对抗网络
nodejs_ 01_ fs. readFile
Driver's license physical examination hospital (114-2 hang up the corresponding hospital driver physical examination)
Greendao reported an error in qigsaw, could not init daoconfig
Deep understanding of C language pointer
C # draw Bezier curve with control points for lattice images and vector graphics
一题多解,ASP.NET Core应用启动初始化的N种方案[上篇]
STM32 simple multi-level menu (array table lookup method)
一篇文章带你走进cookie,session,Token的世界
Talking about the difference between unittest and pytest
My life
Applet global style configuration window
一次 Keepalived 高可用的事故,让我重学了一遍它
Generate confrontation network