diff --git a/example/src/views/xlsxView/demo/xlsxExport/index.md b/example/src/views/xlsxView/demo/xlsxExport/index.md
new file mode 100644
index 0000000..29c95ce
--- /dev/null
+++ b/example/src/views/xlsxView/demo/xlsxExport/index.md
@@ -0,0 +1 @@
+@[js](./src/views/echartsView/demo/BarEchart/index.vue)
diff --git a/example/src/views/xlsxView/demo/xlsxExport/index.vue b/example/src/views/xlsxView/demo/xlsxExport/index.vue
new file mode 100644
index 0000000..5d5e2a6
--- /dev/null
+++ b/example/src/views/xlsxView/demo/xlsxExport/index.vue
@@ -0,0 +1,55 @@
+
+
+
+
+
+
+
+
+
+
+
diff --git a/example/src/views/xlsxView/demo/xlsxImport/index.vue b/example/src/views/xlsxView/demo/xlsxImport/index.vue
index 1b7c22e..b0b6107 100644
--- a/example/src/views/xlsxView/demo/xlsxImport/index.vue
+++ b/example/src/views/xlsxView/demo/xlsxImport/index.vue
@@ -1,14 +1,25 @@
+
+ {{ refData }}
+
diff --git a/example/src/views/xlsxView/index.vue b/example/src/views/xlsxView/index.vue
index 40bafc5..8fdf6f2 100644
--- a/example/src/views/xlsxView/index.vue
+++ b/example/src/views/xlsxView/index.vue
@@ -1,5 +1,11 @@
-
+
+
+
+
+
+
+
@@ -7,6 +13,8 @@
\ No newline at end of file
diff --git a/package.json b/package.json
index f89056e..ae9d408 100644
--- a/package.json
+++ b/package.json
@@ -80,8 +80,5 @@
"lodash-es": "^4.17.21",
"mitt": "^3.0.0",
"xlsx": "^0.18.5"
- },
- "overrides": {
- "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.1/xlsx-0.20.1.tgz"
}
}
diff --git a/src/utils/util.ts b/src/utils/util.ts
index 76a152f..b8b75a1 100644
--- a/src/utils/util.ts
+++ b/src/utils/util.ts
@@ -1,9 +1,11 @@
+import { isDefined } from "@vueuse/core"
+
/**
* @param {T} val
* @param {} defVal=undefined
* 为某个字段创建一个默认值,即undefined
*/
-export function createDef(val: T, defVal: T) {
+export function createDef(val: T, defVal: Exclude) {
return val === undefined || val === defVal ? defVal : val
}
diff --git a/src/xlsx/index.ts b/src/xlsx/index.ts
index 51bdd7b..0b4fd4b 100644
--- a/src/xlsx/index.ts
+++ b/src/xlsx/index.ts
@@ -1,5 +1,5 @@
// 参考文档 https://docs.sheetjs.com/docs
-import { xlsxFileToJson } from "./tools/xlsx-json"
+import { xlsxFileToJson, jsonToXlsxFile } from "./tools/xlsx-json"
-export { xlsxFileToJson }
\ No newline at end of file
+export { xlsxFileToJson, jsonToXlsxFile }
\ No newline at end of file
diff --git a/src/xlsx/tools/xlsx-json.ts b/src/xlsx/tools/xlsx-json.ts
index a39e833..2099227 100644
--- a/src/xlsx/tools/xlsx-json.ts
+++ b/src/xlsx/tools/xlsx-json.ts
@@ -1,23 +1,71 @@
-import { utils } from "xlsx"
+import { createDef } from "howuse/utils/util"
+import { utils, writeFile } from "xlsx"
import { readXlsx } from "./xlsx"
-export function xlsxFileToJson(xlsx: File): Promise> {
+
+export interface XlsxImportOpts {
+ xlsx: File,
+ replaceFields?: Record
+}
+
+export function xlsxFileToJson(opts: XlsxImportOpts): Promise> {
+ const replaceFields = createDef(opts.replaceFields, {})
+
return new Promise((resolve, reject) => {
- readXlsx(xlsx).then(wb => {
+ if (!opts.xlsx) {
+ reject("xlsx is required")
+ return
+ }
+
+ readXlsx(opts.xlsx).then(wb => {
if (wb.SheetNames.length <= 0) {
reject("表格内容为空,请检查...")
}
const sheetJson = {}
for (const sheetName of wb.SheetNames) {
- const data = utils.sheet_to_json(wb.Sheets[sheetName])
+ const data = utils.sheet_to_json(wb.Sheets[sheetName]) as any[]
+ // 替换Excel当中的标题
+ for (const item of data) {
+ for (const [key, value] of Object.entries(item)) {
+ delete item[key]
+ item[replaceFields[key] || key] = value
+ }
+ }
+
sheetJson[sheetName] = data
}
resolve(sheetJson)
- })
+ }).catch(reject)
})
}
-function jsonToXlsx(json: any) {
+// fieldOrder 排序之后的key顺序
+export interface XlsxExportOpts {
+ sheet: { sheetName: string, data: any }[]
+ fileName?: string,
+ replaceFields?: Record
+}
+
+export function jsonToXlsxFile(xlsxData: XlsxExportOpts) {
+ const fileName = createDef(xlsxData.fileName, "demo.xlsx")
+ const replaceFields = createDef(xlsxData.replaceFields, {})
+ // 创建工作簿
+ const wb = utils.book_new();
+
+ for (const { sheetName, data } of xlsxData.sheet) {
+ // 替换表格当中的标题
+ for (const item of data) {
+ for (const [key, value] of Object.entries(item)) {
+ delete item[key]
+ item[replaceFields[key] || key] = value
+ }
+ }
+ const sheet = utils.json_to_sheet(data);
+ // 将工作表放入工作簿中
+ utils.book_append_sheet(wb, sheet, sheetName);
+ // 生成文件并下载
+ }
+ writeFile(wb, fileName);
}
\ No newline at end of file