废话不说,直接上代码
首先前端是一个上传文件的组件,第一步加载文件,第二部导入,在导入的时候就会触发方法对Excel进行解析,转化为json数据!
<p-fileUpload name="file"
accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
chooseLabel="批量申请" uploadLabel="批量导入" cancelLabel="取消导入" customUpload="true"
(uploadHandler)='uploadHandler($event)'>
</p-fileUpload>
下面是实现的前端效果:
下面是实现转换的主要js:
/**
* 将解析的json数据分装到一个临时数组
* @param event
*/
uploadHandler(event) {
this.parseXlsxToJson(event.files[0]).subscribe(resp => {
this.up = resp;
this.up.forEach(element => {
// delete element['xuhao'];
element['字段1'] = e