当前位置:网站首页>Internship: Upload method for writing excel sheet (import)
Internship: Upload method for writing excel sheet (import)
2022-08-03 23:23:00 【ahyo】
After the framework of the entire project is built,For programmers, you only need to start writing the business.比如一般的excel表的导入导出.such as personnelexcel表的导入导出.
以下是导入(把excelData upload realizes data interaction with the database):
It needs to be based on database tables and excelThe table corresponds to the construction of the class,Similar to the following writing form
public class governmentsModel extends Model<governmentsModel> {
private static long serialVersionUID=1L;
@TableField(value = "id",type= IdType.AUTO)
private Integer id;
/** *...... */
@Excel(name = "",width =25 )
@TableField("name")
@ApiModelProperty(value = "")
private String name;
/** *...... */
@Excel(name = "",width =25 )
@TableField("code")
@ApiModelProperty(value = "")
private String code;
/** * */
@TableField("type")
private Integer type;
/** *.... */
@Excel(name = "",width =50 )
@TableField("gb")
@ApiModelProperty(value = "")
private String gb;
/** *..... */
@Excel(name = "",width =50 )
@TableField("category_code")
@ApiModelProperty(value = "")
private String category_code;
....................
}
@excelThe annotations are correspondingexcelThe fields in the table are easy to formexcel表.
After the class is built,Make the write upload method,如下:
/** * 上传人员excel * * @param file * @return */
@RequestMapping(value = "/uploadExcel", method = RequestMethod.POST)
public ResponseData uploadExcel(HttpServletRequest request, MultipartFile file) {
ImportParams importParams = new ImportParams();
importParams.setTitleRows(1);
List<governmentsModel> result;
try {
result = ExcelImportUtil.importExcel(file.getInputStream(), governmentsModel.class, importParams);
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(500, "表格导入错误!");
}
//保存员工信息
if (ToolUtil.isEmpty(result)) {
throw new BusinessException(BizExceptionEnum.EXCEL_EMPTY);
}
List<governmentsModel> errorList = employerService.importExcel(result);
request.getSession().setAttribute("employerList", errorList);
return new ResponseData<>(errorList.size() == 0);
}
Read the code snippet line by line:
1、methodtaken naturallypost Because it is imported, it needs to be uploadedexcel表,ResponseDataThe class is for the convenience of unifying the data format of the front and back ends.
2、HttpServletRequest request, MultipartFile file 是上传excel表时 The necessary parameters for the method request意为请求——服务器请求对象 .MultipartFile——MultipartFile是SpringMVC提供简化上传操作的工具类.
在不使用框架之前,都是使用原生的HttpServletRequest来接收上传的数据,文件是以二进制流传递到后端的,然后需要我们自己转换为File类.使用了MultipartFile工具类之后,我们对文件上传的操作就简便许多了.
3、ImportParams Chinese translation into import parameters 配合 importParams.setTitleRows(1) The number of rows occupied by the header is by default1,Represents that the title occupies one line
4、用集合存储excel表数据,try catch异常捕获 通过ExcelImportUtil工具类 Call the import method——importExcel Pass in file input stream parameters、Use the reflection mechanism to obtain the corresponding base database table sumexcelThe number of rows occupied by the class parameters and headers corresponding to the table are built
5、After judging that it is not empty, the data is saved 至于errorListThe existence of is used to judge whether its field is in the businessnullbe exceptional among themerrorList.size() == 0返回的是布尔类型数据 为trueindicates that it was uploadedexcelThe table is complete and correct.
导出:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletResponse response) {
try {
ExportParams exportParams = new ExportParams("标题:Indicate the intent");
Workbook workbook = ExcelExportUtil.exportExcel(exportParams, governmentsModel.class, new ArrayList<>());
String filename = "信息模板.xls";
response.setContentType("application/force-download");
response.setHeader("Content-disposition", "attachment;filename*=UTF-8''" + URLEncoder.encode(filename, "UTF-8"));
OutputStream ouputStream = response.getOutputStream();
workbook.write(ouputStream);
ouputStream.flush();
ouputStream.close();
} catch (Exception e) {
e.printStackTrace();
throw new BusinessException(BizExceptionEnum.EXCEL_LOAD_WRONG);
}
}
}
边栏推荐
- 2022/8/3 Exam Summary
- FinClip最易用的智能电视小程序
- [Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- Creo 9.0创建几何点
- With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
- Cloud platform construction solutions
- rosbridge-WSL2 && carla-win11
- P1449 后缀表达式
- 【RYU】rest_router.py源码解析
- What is memoization and what is it good for?
猜你喜欢
ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset
Walk the Maze BFS
重发布实验报告
The principle and use of AOSP CameraLatencyHistogram
Unity 截取3D图像 与 画中画PIP的实现
用两个栈模拟队列
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
Zilliz 2023 Fall Campus Recruitment Officially Launched!
直播预告 | 构建业务智联,快速拥抱财务数字化转型
CAS:178744-28-0,mPEG-DSPE,DSPE-mPEG,甲氧基-聚乙二醇-磷脂酰乙醇胺供应
随机推荐
二叉搜索树解决落叶问题
SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
藏宝计划TreasureProject(TPC)系统模式开发技术原理
用队列模拟实现栈
Creo 9.0二维草图的诊断:加亮开放端点
V8中的快慢数组(附源码、图文更易理解)
用两个栈模拟队列
伴随着元宇宙、web3.0等概念的兴起,数字人、数字场景等诸多数字化的形态开始出现
七夕活动浪漫上线,别让网络拖慢和小姐姐的开黑时间
直播系统聊天技术(八):vivo直播系统中IM消息模块的架构实践
Analysys Analysis: The transaction scale of China's online retail B2C market in Q2 2022 will reach 2,344.47 billion yuan
golang写的存储引擎,基于b+树,mmap
[RYU] rest_router.py source code analysis
FinClip,助长智能电视更多想象空间
智能座舱的「交互设计」大战
The longest substring that cannot have repeating characters in a leetcode/substring
The salary of soft testers at each stage, come to Kangkang, how much can you get?
Software testing is seriously involution, how to improve your competitiveness?
用两个栈模拟队列
Pytest学习-setup/teardown