当前位置:网站首页>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);
}
}
}
边栏推荐
- 智能座舱的「交互设计」大战
- 1067 Sort with Swap(0, i)
- 创建函数报错,提示DECLARE定义语法问题
- 雅思大作文写作模版
- RPA助力商超订单自动化!
- Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
- 牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
- log4j-slf4j-impl cannot be present with log4j-to-slf4j
- 使用tf.image.resize() 和tf.image.resize_with_pad()调整图像大小
- V8中的快慢数组(附源码、图文更易理解)
猜你喜欢
SPOJ 2774 Longest Common Substring(两串求公共子串 SAM)
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
Pytest学习-skip/skipif
Shell编程之循环语句与函数
Fluorescein-PEG-CLS, cholesterol-polyethylene glycol-fluorescein scientific research reagent
Software testing is seriously involution, how to improve your competitiveness?
override learning (parent and child)
简单了解下 TCP,学习握手和挥手以及各种状态到底是怎么样的
rosbridge-WSL2 && carla-win11
Creo 9.0二维草图的诊断:加亮开放端点
随机推荐
curl使用指南
ts用法大全
冰河又一MySQL力作出版(文末送书)!!
Take an example of a web worker
V8中的快慢数组(附源码、图文更易理解)
689. 三个无重叠子数组的最大和
First domestic open source framework 】 【 general cloud computing framework, any program can be made into cloud computing.
【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
【RYU】rest_router.py源码解析
Click the icon in Canvas App to generate PDF and save it to Dataverse
2022/8/3 考试总结
MCS-51单片机,定时1分钟,汇编程序
剑指offer第22题-链表中倒数第K个节点
设置工作模式与环境(下):探查和收集信息
用两个栈模拟队列
Unity2021发布WebGL雾效消失问题
Unity 截取3D图像 与 画中画PIP的实现
log4j-slf4j-impl cannot be present with log4j-to-slf4j
override学习(父类和子类)
直播预告 | 构建业务智联,快速拥抱财务数字化转型