当前位置:网站首页>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);
}
}
}
边栏推荐
- Pytest学习-setup/teardown
- AOSP CameraLatencyHistogram的原理与使用
- ML之yellowbrick:基于titanic泰坦尼克是否获救二分类预测数据集利用yellowbrick对LoR逻辑回归模型实现可解释性(阈值图)案例
- 七夕?new一个对象
- 禾匠编译错误记录
- utils 定时器
- 2022/8/3 考试总结
- FinClip,助长智能电视更多想象空间
- ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
- Unity2021 releases WebGL fog effect disappearing problem
猜你喜欢

Click the icon in Canvas App to generate PDF and save it to Dataverse

用两个栈模拟队列

【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve

Creo 9.0二维草图的诊断:着色封闭环

射频芯片ATE测试从入门到放弃之参数测试

Pytest学习-skip/skipif

Creo 9.0在草图环境中创建坐标系

ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例

藏宝计划TreasureProject(TPC)系统模式开发技术原理

【深度学习】基于tensorflow的服装图像分类训练(数据集:Fashion-MNIST)
随机推荐
牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
Scala basics [regular expressions, framework development principles]
响应式织梦模板餐饮酒店类网站
689. 三个无重叠子数组的最大和
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
Live Preview | Build Business Intelligence, Quickly Embrace Financial Digital Transformation
Shell编程之循环语句与函数
射频芯片ATE测试从入门到放弃之参数测试
超级完美版布局有快捷键,有背景置换(解决opencv 中文路径问题)
Pytest学习-skip/skipif
3D Semantic Segmentation - 2DPASS
Flutter教程之为什么 Flutter 是创业的最佳选择?
Republish the lab report
密码学基础以及完整加密通讯过程解析
Why do we need callbacks
libnet
RPA power business automation super order!
Websocket multi-threaded sending message error TEXT_PARTIAL_WRITING--Use case of spin lock replacing synchronized exclusive lock
IELTS essay writing template
The longest substring that cannot have repeating characters in a leetcode/substring