123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- 'use strict';
- const XLSX = require('xlsx');
- const Service = require('egg').Service;
- class ExcelimportService extends Service {
- constructor(ctx) {
- super(ctx);
- this.appid = '';
- }
- // 获取导入的XLSX文件中的数据
- async getImportXLSXData(result) {
- for (const elem of result) {
- const id = elem._id;
- const filepath = this.ctx.app.config.fileDirImp + elem.content;
- if(elem.type === '0'){
- const workbook = XLSX.readFile(filepath);
- // 读取内容
- let exceldata = [];
- const sheetNames = workbook.SheetNames; // 获取表名
- const sheet = workbook.Sheets[sheetNames[0]]; // 通过表名得到表对象
- const theadRule = [ sheet.A1.v, sheet.B1.v, sheet.C1.v, sheet.D1.v, sheet.E1.v, sheet.F1.v, sheet.G1.v, sheet.H1.v, sheet.I1.v, sheet.J1.v, sheet.K1.v, sheet.L1.v, sheet.M1.v, sheet.N1.v, sheet.O1.v, sheet.P1.v ];
- const params = XLSX.utils.sheet_to_json(sheet); // 通过工具将表对象的数据读出来并转成json
- // const theadRule = [ '序号', '院系', '班级', '专业代码', '专业名称', '学号', '姓名', '身份证号', '性别', '学历代码', '学历名称', '毕业年份', '民族', '电话号码', '生源所在地', '政治面貌' ];
- console.log(params);
- if (!params) return [];
- let i = 0;
- const length = params.length;
- const _datas = [];
- let data = {};
- for (i; i < length; i++) {
- data = params[i];
- _datas.push({
- szyx: data[theadRule[1]],
- szbj: data[theadRule[2]],
- zydm: data[theadRule[3]],
- zymc: data[theadRule[4]],
- xh: data[theadRule[5]],
- xm: data[theadRule[6]],
- sfzh: data[theadRule[7]],
- xb: data[theadRule[8]],
- xldm: data[theadRule[9]],
- xl: data[theadRule[10]],
- year: data[theadRule[11]],
- mz: data[theadRule[12]],
- dhhm: data[theadRule[13]],
- syszd: data[theadRule[14]],
- zzmm: data[theadRule[15]],
- });
- }
- exceldata = [ ...exceldata, ..._datas ];
- console.log(exceldata);
- //
- const stuPath = this.ctx.app.config.baseDirImp + this.ctx.app.config.stusDirImp;
- const queryData = { year: elem.createtime, schid: elem.userid, schname: elem.name};
- const studRes = this.ctx.curl(stuPath, {
- method: 'POST',
- dataType: 'json',
- query: queryData,
- body: exceldata,
- });
- console.log(studRes);
- // 导入成功时更新状态
- if(studRes != null){
- const updatedata = {id: id, status: '1' };
- await this.ctx.service.dataimp.update(updatedata);
- }
- }
- }
-
- return '0';
- }
- }
- module.exports = ExcelimportService;
|