bl-common-vue3 3.8.108 → 3.8.109
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/package.json
CHANGED
package/src/common/utils/xlsx.js
CHANGED
|
@@ -1,6 +1,60 @@
|
|
|
1
1
|
import fs from "file-saver";
|
|
2
2
|
import * as XLSX from "kj-xlsx";
|
|
3
|
-
|
|
3
|
+
|
|
4
|
+
export function getVerticalMergeCells(json, fields, options = {}) {
|
|
5
|
+
const { groupField, mergeFields = [] } = options;
|
|
6
|
+
if (!groupField || !Array.isArray(mergeFields) || !mergeFields.length) {
|
|
7
|
+
return [];
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
const fieldKeys = Object.keys(fields);
|
|
11
|
+
const mergeColumnIndexes = mergeFields
|
|
12
|
+
.map((field) => fieldKeys.indexOf(field))
|
|
13
|
+
.filter((columnIndex) => columnIndex >= 0);
|
|
14
|
+
if (!mergeColumnIndexes.length) {
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const merges = [];
|
|
19
|
+
let groupStartIndex = 0;
|
|
20
|
+
|
|
21
|
+
const appendGroupMerges = (groupEndIndex) => {
|
|
22
|
+
const groupValue = json[groupStartIndex]?.[groupField];
|
|
23
|
+
if (
|
|
24
|
+
groupEndIndex <= groupStartIndex ||
|
|
25
|
+
groupValue === undefined ||
|
|
26
|
+
groupValue === null ||
|
|
27
|
+
groupValue === ""
|
|
28
|
+
) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
mergeColumnIndexes.forEach((columnIndex) => {
|
|
33
|
+
merges.push({
|
|
34
|
+
// 工作表第 0 行是表头,数据行需要整体向下偏移一行。
|
|
35
|
+
s: { r: groupStartIndex + 1, c: columnIndex },
|
|
36
|
+
e: { r: groupEndIndex + 1, c: columnIndex },
|
|
37
|
+
});
|
|
38
|
+
});
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
for (let rowIndex = 1; rowIndex <= json.length; rowIndex++) {
|
|
42
|
+
const groupChanged =
|
|
43
|
+
rowIndex === json.length ||
|
|
44
|
+
json[rowIndex]?.[groupField] !== json[groupStartIndex]?.[groupField];
|
|
45
|
+
if (!groupChanged) {
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
appendGroupMerges(rowIndex - 1);
|
|
50
|
+
groupStartIndex = rowIndex;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
return merges;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function xlsx(json, fields, filename = ".xlsx", mergeOptions = {}) {
|
|
57
|
+
const merges = getVerticalMergeCells(json, fields, mergeOptions);
|
|
4
58
|
//导出xlsx
|
|
5
59
|
json.forEach((item) => {
|
|
6
60
|
for (let i in item) {
|
|
@@ -14,6 +68,9 @@ export function xlsx(json, fields, filename = ".xlsx") {
|
|
|
14
68
|
let sheetName = filename.replace(/[\\/?*[\]:]/g, "").slice(0, 31) || "Sheet1"; //excel的文件名称
|
|
15
69
|
let wb = XLSX.utils.book_new(); //工作簿对象包含一SheetNames数组,以及一个表对象映射表名称到表对象。XLSX.utils.book_new实用函数创建一个新的工作簿对象。
|
|
16
70
|
let ws = XLSX.utils.json_to_sheet(json, { header: Object.values(fields) }); //将JS对象数组转换为工作表。
|
|
71
|
+
if (merges.length) {
|
|
72
|
+
ws["!merges"] = merges;
|
|
73
|
+
}
|
|
17
74
|
wb.SheetNames.push(sheetName);
|
|
18
75
|
wb.Sheets[sheetName] = ws;
|
|
19
76
|
const defaultCellStyle = {
|
|
@@ -67,6 +67,10 @@ export default defineComponent({
|
|
|
67
67
|
type: Object,
|
|
68
68
|
default: () => ({})
|
|
69
69
|
},
|
|
70
|
+
merge_options: { //导出表格纵向合并配置
|
|
71
|
+
type: Object,
|
|
72
|
+
default: () => ({})
|
|
73
|
+
},
|
|
70
74
|
custom_result: { //导出表格头部
|
|
71
75
|
type: Object,
|
|
72
76
|
// eslint-disable-next-line vue/require-valid-default-prop
|
|
@@ -123,7 +127,7 @@ export default defineComponent({
|
|
|
123
127
|
console.log('导出文档')
|
|
124
128
|
setTimeout(() => {
|
|
125
129
|
let list = utils.deepClone(state.ExportList);
|
|
126
|
-
xlsx(list, removeUndefined(props.columnstitle), props.title);
|
|
130
|
+
xlsx(list, removeUndefined(props.columnstitle), props.title, props.merge_options);
|
|
127
131
|
context.emit("success", t('ExportComponents.index.921182-2'));
|
|
128
132
|
closeModal()
|
|
129
133
|
}, 200);
|
|
@@ -140,7 +144,7 @@ export default defineComponent({
|
|
|
140
144
|
console.log('导出文档')
|
|
141
145
|
setTimeout(() => {
|
|
142
146
|
let list = utils.deepClone(state.ExportList);
|
|
143
|
-
xlsx(list, removeUndefined(props.columnstitle), props.title);
|
|
147
|
+
xlsx(list, removeUndefined(props.columnstitle), props.title, props.merge_options);
|
|
144
148
|
context.emit("success", t('ExportComponents.index.921182-2'));
|
|
145
149
|
closeModal()
|
|
146
150
|
}, 200);
|
|
@@ -160,7 +164,7 @@ export default defineComponent({
|
|
|
160
164
|
(val) => {
|
|
161
165
|
setTimeout(() => {
|
|
162
166
|
let list = utils.deepClone(val);
|
|
163
|
-
xlsx(list, removeUndefined(props.columnstitle), props.title);
|
|
167
|
+
xlsx(list, removeUndefined(props.columnstitle), props.title, props.merge_options);
|
|
164
168
|
context.emit("success", t('ExportComponents.index.921182-2'));
|
|
165
169
|
closeModal()
|
|
166
170
|
}, 200);
|