zxkv-xlsx-json
v0.3.1
Published
xlsx to json
Downloads
3
Readme
zxkv-xlsx-json
Description
前端解析 xlsx 格式的 excel 文件,解析数据转换为 JSON 数据格式(仅支持 xlsx)
Install
你可以使用 yarn、npm、pnpm 等包管理工具进行安装; 安装方式如下:
yarn add zxkv-xlsx-json
or
npm install zxkv-xlsx-json
or
pnpm add zxkv-xlsx-json
Using
引入方式:
import XTJ from "zxkv-xlsx-json";
methods
| 方法 | 介绍 | | ---- | ------------------------------ | | XTJ | xlsx 格式的 excel 文件数据解析 |
[excel文件数据解析] - 固定列表项
/**
* @description xlsx 转 json
* @param {Object} file 本地文件对象
* @return {Object} 解析成功时返回 Array 类型的数组,解析失败时 返回 null
* 注:非 excel 文件会解析失败,返回 null
*/
const parseJSON = async file => {
// key: 数据表头列表项、prop:对应匹配的键值
let stmpKeys = {
姓名: {
prop: "stuName",
type: String
},
性别: {
prop: "stuSix",
type: String
},
学号: {
prop: "stuNo",
type: Number
}
};
// 注:列表项需唯一
// 数据项默认值
let stmpVals = {
stuName: "",
stuSix: "",
stuNo: ""
};
// 列表表头前的描述所占行数,默认 0,即:head 在第一行
let descValue = 0;
// 当前方法支持 xlsx 格式的 excel 文件解析
let list = await XTJ(file, stmpKeys, stmpVals, descValue);
if (Array.isArray(list)) {
// 解析成功
console.log("文件解析成功");
} else {
// 解析失败
console.warn("文件解析失败");
}
};
[数据格式] - 固定项
[
{
stuName: "Ming",
stuSix: "boy",
stuNo: "stu001"
},
{
stuName: "Hong",
stuSix: "girl",
stuNo: "stu002"
},
......
];
[excel文件数据解析] - 动态列表项
/**
* @description xlsx 转 json
* @param {Object} file 本地文件对象
* @return {Object} 解析成功时返回 Array 类型的数组,解析失败时 返回 null
* 注:非 excel 文件会解析失败,返回 null
*/
const parseJSON = async file => {
// 列表项不固定
let stmpKeys = {};
// 数据项默认值
let stmpVals = {};
// 列表表头前的描述所占行数,默认 0,即:head 在第一行
let descValue = 0;
// 当前方式仅支持 xlsx 格式的 excel 文件解析
let {head, body} = await XTJ(file, {}, {}, descValue, true);
/**
* head:表头
* 格式:['姓名','性别','学号']
* body:数据体
* 格式:[['小明','男',null],['小红','女','stu001']]
**/
let { head, body } = data;
// ... 依据返回数据处理成符合自己要求的数据格式即可
};
Ends
⏰ tips:基于 Demo 案例重新构建,有好的建议或使用过程中发现问题,可邮件联系。
📮 email:[email protected]