当前位置:网站首页>internship:编写excel表的上传方法(导入)
internship:编写excel表的上传方法(导入)
2022-08-03 23:13:00 【ahyo】
整个项目的框架搭建好之后,对于程序员只需要着手于业务的编写即可。比如一般的excel表的导入导出。比如人员的excel表的导入导出。
以下是导入(把excel数据上传实现与数据库的数据交互):
需要依据数据库表和excel表对应起来建类,类似于以下的编写形式
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;
....................
}
@excel的注解就是在对应着excel表里的字段方便形成excel表。
类建好之后,进行编写上传方法,如下:
/** * 上传人员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);
}
逐行解读代码段:
1、method自然采取post 因为是导入需要上传excel表,ResponseData类是为了方便统一前后端的数据格式。
2、HttpServletRequest request, MultipartFile file 是上传excel表时 方法必要的传参 request意为请求——服务器请求对象 。MultipartFile——MultipartFile是SpringMVC提供简化上传操作的工具类。
在不使用框架之前,都是使用原生的HttpServletRequest来接收上传的数据,文件是以二进制流传递到后端的,然后需要我们自己转换为File类。使用了MultipartFile工具类之后,我们对文件上传的操作就简便许多了。
3、ImportParams 中文译为导入参数 配合 importParams.setTitleRows(1) 表头所占据的行数默认1,代表标题占据一行
4、用集合存储excel表数据,try catch异常捕获 通过ExcelImportUtil工具类 调用导入方法——importExcel 传入文件输入流参数、利用反射机制获取对应的依据数据库表和excel表对应起来建的类参数和标题占据行数
5、判断不为空之后便保存完数据 至于errorList的存在是用于在业务中判断其字段是否为null予以异常其中errorList.size() == 0返回的是布尔类型数据 为true则表明该上传的excel表完整无误。
导出:
@RequestMapping(value = "/download", method = RequestMethod.GET)
public void download(HttpServletResponse response) {
try {
ExportParams exportParams = new ExportParams("标题:指明用意");
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);
}
}
}
边栏推荐
- With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
- Create function report error, prompting DECLARE definition syntax problem
- 工作小计 QT打包
- 生成器版和查看器版有什么区别?
- Creo9.0 绘制中心线
- 响应式织梦模板塑身瑜伽类网站
- RPA power business automation super order!
- Network basic learning series four (network layer, data link layer and some other important protocols or technologies)
- ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
- 软件测试内卷严重,如何提升自己的竞争力呢?
猜你喜欢

(PC+WAP)织梦模板螺钉手柄类网站

FinClip最易用的智能电视小程序
![[MySQL Advanced] Creation and Management of Databases and Tables](/img/31/2015122e409148b3679b09a03db869.png)
[MySQL Advanced] Creation and Management of Databases and Tables

Binary search tree to solve the fallen leaves problem

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

【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。

Boss: There are too many systems in the company, can you realize account interoperability?

用栈实现队列

密码学基础以及完整加密通讯过程解析

Embedded systems: overview
随机推荐
用两个栈模拟队列
(PC+WAP)织梦模板螺钉手柄类网站
Walk the Maze BFS
Quickly build a website with static files
utlis thread pool
物联网新零售模式,引领购物新潮流
用队列模拟实现栈
Another MySQL masterpiece published by Glacier (send the book at the end of the article)!!
With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
RPA power business automation super order!
HDU 5655 CA Loves Stick
AOSP CameraLatencyHistogram的原理与使用
Interpretation of ML: A case of global interpretation/local interpretation of EBC model interpretability based on titanic titanic rescued binary prediction data set using interpret
栈的压入、弹出序列
Unity2021发布WebGL雾效消失问题
BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
Why do we need callbacks
创建函数报错,提示DECLARE定义语法问题
密码学基础以及完整加密通讯过程解析
FinClip最易用的智能电视小程序