doomiwork 4.0.7 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/core/controller.js +10 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/utilities/excelutility.js +85 -22
package/core/controller.js
CHANGED
|
@@ -54,6 +54,16 @@ class controller {
|
|
|
54
54
|
let excelResult = await exportUtility.recordset2ExcelFile(data,excelkey, this.beforeExportExcel)
|
|
55
55
|
return excelResult;
|
|
56
56
|
}
|
|
57
|
+
/**
|
|
58
|
+
* 导出数据到Excel的多个workseet中
|
|
59
|
+
* @param {*} data
|
|
60
|
+
* @param {*} excelkey
|
|
61
|
+
*/
|
|
62
|
+
async export2MutlisheetExcel(datainfo) {
|
|
63
|
+
if (!Array.isArray(datainfo) || datainfo.length == 0) return {successed:false};
|
|
64
|
+
let excelResult = await exportUtility.recordset2MutlisheetExcelFile(datainfo, this.beforeExportExcel)
|
|
65
|
+
return excelResult;
|
|
66
|
+
}
|
|
57
67
|
/**
|
|
58
68
|
* 写入Excel数据
|
|
59
69
|
* @param {*} data
|
package/index.js
CHANGED
|
@@ -4,6 +4,7 @@ exports = module.exports = {
|
|
|
4
4
|
model: require('./core/database/daoBase'),
|
|
5
5
|
appsetting:require('./configuration/appsetting'),
|
|
6
6
|
actionresult:require('./core/actionresult'),
|
|
7
|
+
excelutility:require('./utilities/excelutility'),
|
|
7
8
|
tokenHelper:require('./utilities/tokenutility'),
|
|
8
9
|
constants:require('./core/enumconst'),
|
|
9
10
|
}
|
package/package.json
CHANGED
|
@@ -35,7 +35,7 @@ class ExcelUtility {
|
|
|
35
35
|
});
|
|
36
36
|
fReadStream = fs.createReadStream(buffer);
|
|
37
37
|
fReadStream.on("data", (chunk) => res.write(chunk, "binary"));
|
|
38
|
-
fReadStream.on("end",
|
|
38
|
+
fReadStream.on("end", () => {
|
|
39
39
|
res.end();
|
|
40
40
|
});
|
|
41
41
|
}
|
|
@@ -43,23 +43,17 @@ class ExcelUtility {
|
|
|
43
43
|
async writeExcel(data, workSheetName) {
|
|
44
44
|
let sheetName = 'sheet1';
|
|
45
45
|
if (workSheetName == null) sheetName = workSheetName;
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
fs.writeFileSync(fileName, buffer, { 'flag': 'w' });
|
|
58
|
-
return { successed: true, fileName: randomFile };
|
|
59
|
-
}
|
|
60
|
-
catch (err) {
|
|
61
|
-
return { successed: false, errorcode: 1, errmessage: err };
|
|
62
|
-
}
|
|
46
|
+
let randomFile = uuid.v4() + '.xlsx';
|
|
47
|
+
let fileName = path.resolve(this.exportFolder, randomFile);
|
|
48
|
+
let _saveDir = path.dirname(fileName);
|
|
49
|
+
///创建本地文件夹
|
|
50
|
+
if (!this.mkdirsSync(_saveDir)) return { success: false, errorcode: 1, errmessage: 'create folder failed!' };
|
|
51
|
+
const buffer = xlsx.build([{ name: sheetName, data: data }]);
|
|
52
|
+
return new Promise(reslove => {
|
|
53
|
+
return fs.writeFile(fileName, buffer, (err) => {
|
|
54
|
+
return reslove({ successed: !err, errmessage: err, fileName:randomFile })
|
|
55
|
+
})
|
|
56
|
+
})
|
|
63
57
|
}
|
|
64
58
|
///导出记录集到Excel,写入
|
|
65
59
|
///dataRow :记录集
|
|
@@ -69,9 +63,9 @@ class ExcelUtility {
|
|
|
69
63
|
let _instance = this;
|
|
70
64
|
//var setting = exportConfig ? excelConfig[exportConfig] : null;
|
|
71
65
|
let setting = exportConfig ? _instance.excelConfig[exportConfig] : null;
|
|
72
|
-
let dataBuffer = [],worksheetName = "sheet1", fieldKey = [], titleRow = [];
|
|
66
|
+
let dataBuffer = [], worksheetName = "sheet1", fieldKey = [], titleRow = [];
|
|
73
67
|
if (setting != null) {
|
|
74
|
-
worksheetName = setting.worksheet
|
|
68
|
+
worksheetName = setting.worksheet ?? "sheet1"; //== null ? "sheet1" : setting.worksheet;
|
|
75
69
|
////第一行导出标题行
|
|
76
70
|
setting.columns.forEach((field) => {
|
|
77
71
|
var dataOption = { type: 'title', config: exportConfig, title: field.title, field: field.name };
|
|
@@ -108,6 +102,75 @@ class ExcelUtility {
|
|
|
108
102
|
return result;
|
|
109
103
|
}
|
|
110
104
|
|
|
105
|
+
///写Excel文件
|
|
106
|
+
async writeMutlisheetExcel(sheetinfo) {
|
|
107
|
+
//let sheetName = 'sheet1';
|
|
108
|
+
//if (workSheetName == null) sheetName = workSheetName;
|
|
109
|
+
let randomFile = uuid.v4() + '.xlsx';
|
|
110
|
+
let fileName = path.resolve(this.exportFolder, randomFile);
|
|
111
|
+
let _saveDir = path.dirname(fileName);
|
|
112
|
+
///创建本地文件夹
|
|
113
|
+
if (!this.mkdirsSync(_saveDir)) return { success: false, errorcode: 1, errmessage: 'create folder failed!' };
|
|
114
|
+
const buffer = xlsx.build(sheetinfo);// ([{ name: sheetName, data: data }]);
|
|
115
|
+
return new Promise(reslove => {
|
|
116
|
+
return fs.writeFile(fileName, buffer, (err) => {
|
|
117
|
+
return reslove({ successed: !err, errmessage: err, fileName: randomFile })
|
|
118
|
+
})
|
|
119
|
+
})
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 导出记录集到Excel的多个worksheet中
|
|
123
|
+
* @param {*} dataRow
|
|
124
|
+
* @param {*} exportConfig
|
|
125
|
+
* @param {*} readyDataFunc
|
|
126
|
+
* @returns
|
|
127
|
+
*/
|
|
128
|
+
async recordset2MutlisheetExcelFile(sheetsInfo, readyDataFunc) {
|
|
129
|
+
let _instance = this;
|
|
130
|
+
let sheetinfo = [];
|
|
131
|
+
for (const { data: dataRow, mapping: exportConfig } of sheetsInfo){
|
|
132
|
+
let setting = exportConfig ? _instance.excelConfig[exportConfig] : null;
|
|
133
|
+
let dataBuffer = [], worksheetName = "sheet1", fieldKey = [], titleRow = [];
|
|
134
|
+
if (setting != null) {
|
|
135
|
+
worksheetName = setting.worksheet == null ? "sheet1" : setting.worksheet;
|
|
136
|
+
////第一行导出标题行
|
|
137
|
+
setting.columns.forEach((field) => {
|
|
138
|
+
var dataOption = { type: 'title', config: exportConfig, title: field.title, field: field.name };
|
|
139
|
+
if (readyDataFunc != null && typeof (readyDataFunc) == "function")
|
|
140
|
+
dataOption = readyDataFunc(dataOption)
|
|
141
|
+
titleRow.push(dataOption.title);
|
|
142
|
+
fieldKey.push(dataOption.field);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
else if (dataRow.length > 0) {
|
|
146
|
+
var firstRow = dataRow[0];
|
|
147
|
+
Object.keys(firstRow).forEach(key => {
|
|
148
|
+
var dataOption = { type: 'title', config: null, title: key, field: key };
|
|
149
|
+
if (readyDataFunc != null && typeof (readyDataFunc) == "function")
|
|
150
|
+
dataOption = readyDataFunc(dataOption)
|
|
151
|
+
titleRow.push(dataOption.title);
|
|
152
|
+
fieldKey.push(dataOption.field);
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
if (titleRow.length > 0) {
|
|
156
|
+
dataBuffer.push(titleRow);
|
|
157
|
+
dataRow.forEach((rowItem) => {
|
|
158
|
+
var dataRow = [];
|
|
159
|
+
fieldKey.forEach((field) => {
|
|
160
|
+
var dataOption = { type: 'data', config: exportConfig, field: field, data: rowItem, value: rowItem[field] };
|
|
161
|
+
if (readyDataFunc != null && typeof (readyDataFunc) == "function")
|
|
162
|
+
dataOption = readyDataFunc(dataOption)
|
|
163
|
+
dataRow.push(dataOption.value);
|
|
164
|
+
});
|
|
165
|
+
dataBuffer.push(dataRow);
|
|
166
|
+
})
|
|
167
|
+
}
|
|
168
|
+
sheetinfo.push({ name: worksheetName, data: dataBuffer });
|
|
169
|
+
}
|
|
170
|
+
const result = await _instance.writeMutlisheetExcel(sheetinfo);
|
|
171
|
+
return result;
|
|
172
|
+
}
|
|
173
|
+
|
|
111
174
|
/**
|
|
112
175
|
* 递归创建多级目录
|
|
113
176
|
* @param {*} dirpath
|
|
@@ -116,8 +179,8 @@ class ExcelUtility {
|
|
|
116
179
|
*/
|
|
117
180
|
mkdirsSync(dirpath, mode) {
|
|
118
181
|
if (!fs.existsSync(dirpath)) {
|
|
119
|
-
|
|
120
|
-
|
|
182
|
+
let pathtmp;
|
|
183
|
+
const splitPath = dirpath.split(path.sep);
|
|
121
184
|
for (var nLoop = 0; nLoop < splitPath.length; nLoop++) {
|
|
122
185
|
var dirname = splitPath[nLoop];
|
|
123
186
|
if (dirname.length == 0) {
|