使用xlsx.full.min.js 获取excel的日期数据为:37858;
拿到的整数值是日期距离1900年1月1日的天数,这时需要写一个函数转换:
/**
* excel导入插件方法:excel时间格式化
* @param {*} numb excel时间格式
* @param {*} format 转换分隔符
* @returns 标准时间格式
*/
export function formatExcelDate(numb, format) {
// 天数
const time = new Date((numb - 1) * 24 * 3600000 + 1)
console.log(time)
time.setYear(time.getFullYear() - 70)
const year = time.getFullYear() + ''
const month = time.getMonth() + 1 + ''
const date = time.getDate() + ''
// 转换的格式符号
if (format && format.length === 1) {
return new Date(year + format + month + format + date)
}
return (
year + (month < 10 ? '0' + month : month) + (date < 10 ? '0' + date : date)
)
}
参数:numb是excel转换出来的整数值,format是年月日之间分隔符号。
将excel表格中的中文替换英文
/**
* excel导入插件方法:key中文转英文
* @param {*} results 需要转换的数据
* @param {*} maps 转换数据的中英映射关系
* @param {*} times 需要格式化的excel时间属性名
* @returns
*/
export function transformKeys(results, maps, times = []) {
const newArr = []
results.forEach((item) => {
// 替换为key是英文的新对象,添加进数组
const _item = {}
for (const key in item) {
const enKey = maps[key]
if (times.includes(enKey)) {
// 格式化excel时间
_item[enKey] = formatExcelDate(item[key], '-')
} else {
_item[enKey] = item[key]
}
}
newArr.push(_item)
})
return newArr
}