Leanote's Blog
I love Leanote!
Toggle navigation
Leanote's Blog
Home
Chrome
Git
Linux
Windows
Others
工具大全
VsCode
Expo
Html
JavaScript
Npm
Node
Mock
React-Native
React
TypeScript
小程序
插件
正则
Dva
Ant-Design-React
Umi
Vue
Vux
Ant-Design-Vue
Http
Java
flutter
开发小工具
About Me
Archives
Tags
XLSX
2019-11-14 07:46:38
8
0
0
admin
## 生成xlsx文件 #### createXLSX.js ``` /** * create time: 18/6/2019 * author: Peng */ import XLSX from "xlsx" import { message } from "antd" /** * * @param {Array} msgArr * 例子: * 表头 * const headers = ['id', 'name', 'age', 'country']; * 表格数据 * const data = [ * { * id: '1', * name: 'test1', * age: '30', * country: 'China', * }, * ]; * @param {String} outputName * default = "output" */ export const createXLSX = ({ msgArr = [], outputName = "output" }) => { console.log(msgArr) if (!msgArr.length) { message.warning("需要下载的数据为空") return } const headers = Object.keys(msgArr[0]) const newHeaders = headers .map((v, i) => Object.assign({}, { v: v, position: String.fromCharCode(65 + i) + 1 })) .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {}); const newData = msgArr .map((v, i) => headers.map((k, j) => Object.assign({}, { v: v[k], position: String.fromCharCode(65 + j) + (i + 2) }))) .reduce((prev, next) => prev.concat(next)) .reduce((prev, next) => Object.assign({}, prev, { [next.position]: { v: next.v } }), {}); const output = Object.assign({}, newHeaders, newData); // 获取所有单元格的位置 const outputPos = Object.keys(output); // 计算出范围 const ref = outputPos[0] + ':' + outputPos[outputPos.length - 1]; // 构建 workbook 对象 const workbook = { SheetNames: ['mySheet'], Sheets: { 'mySheet': Object.assign({}, output, { '!ref': ref }) } }; // 导出 Excel XLSX.writeFile(workbook, outputName + '.xlsx') } ``` #### 使用 ``` import { createXLSX } from "createXLSX.js" createXLSX(xlsxArr) ```
Pre:
轨迹展示-海量数据-高德地图API
Next:
0
likes
8
Weibo
Wechat
Tencent Weibo
QQ Zone
RenRen
Submit
Sign in
to leave a comment.
No Leanote account?
Sign up now.
0
comments
More...
Table of content
No Leanote account? Sign up now.