前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法

02-27 1632阅读 0评论

使用 xlsx 读取 excel 的时间格式的数据时,如 ‘2023-11-30’,‘2023/11/30’ ,默认会读取一串数字字符串,如:‘45260’,此时需要在 read 的时候传入一个配置项:

前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,中国,前端,第1张
(图片来源网络,侵删)
import { read } from 'xlsx'
const workbook = read(fileData, {
    type: 'binary',
    cellDates: true, // 读取日期格式的数据
})

此时拿到的是标准的时间格式 :‘Wed Nov 29 2023 23:59:17 GMT+0800(中国标准时间)’ ,这个时间格式是带时区的,有没有发现,只要输入年月日,读到的数据总是差 43 秒,解决思路也很粗暴,判断是这个时间,直接加 44 秒。

if(dateStr){
    if(dateStr?.includes('23:59:17')) {
        dateStr = dayjs(dateStr).add(44, 'second')
    }
    // 如果需要可以格式化成需要的格式
    const dayObj = dayjs(dateStr.toString())
    if(dayObj.isValid()) {
        dateStr = dayObj.format('YYYY-MM-DD')
    }
    return dateStr
}

附:element-plus el-upload 读取 xlsx 格式的 excel 文件的步骤

  
    导入
  
  
导出 import { ref } from 'vue' import type { UploadFile, UploadRawFile } from 'element-plus' import { read, utils, writeFile } from 'xlsx' type IExcel = Record const fileList = ref([]) const importData = ref(null) async function onSelectFile(file: UploadFile) { reset() if(file.raw) { if(file.raw.type !== 'application/vnd.openxmlformats-offocedocument.spreadsheetml.sheet') { return '请上传 xlsx 格式文件' } if(file.raw.size / 1024 / 1024 > 10) { return '文件格式不能超过 10M' } fileList.value.push({ name: file.raw.name }) // 解析文件 const raw = file.raw const res = await readFile2Binary(raw) const resInfo: IExcel = {} // 解析结果 if(res) { const workbook = read(res, { type: 'binary', cellDates: true, }) workbook.SheetNames.forEach((sheetName) => { const excelData: Record[] = utils.sheet_to_json(workbook.Sheets[sheetName]) resInfo[sheetName] = excelData }) // 检查数据的合法性 // if(validXLSX(resInfo)) { // importData.value = resInfo // } importData.value = resInfo } } } // 重置 function reset() { fileList.value = [] // ... } function onRemoveFile() { reset() } /** * 将 el-upload 选择的文件读取成二进制 * @param raw */ function readFile2Binary(raw: UploadRawFile) { return new Promise((resolve, reject) => { const reader = new FileReader() reader.readAsBinaryString(raw) reader.onload = (ev) => { if(ev.target) { resolve(ev.target.result) } else { reject() } } }) } /** * 导出 */ function handleExport() { const sheetList = { sheet1: [], sheet2: [], } const fileName = 'xxx.xlsx' const workbook = utils.book_new() for(const key in sheetList) { const sheetName = key const worksheet = utils.aoa_to_sheet(sheetList[key]) utils.book_append_sheet(workbook, worksheet,sheetName) } writeFile(workbook, fileName, { bookType: 'xlsx', }) }
前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,中国,前端,第2张
(图片来源网络,侵删)
前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,前端使用 xlsx.js 工具读取 excel 遇到时间日期少 43 秒的解决办法,词库加载错误:未能找到文件“C:\Users\Administrator\Desktop\火车头9.8破解版\Configuration\Dict_Stopwords.txt”。,使用,中国,前端,第3张
(图片来源网络,侵删)

免责声明
本网站所收集的部分公开资料来源于AI生成和互联网,转载的目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。
文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

发表评论

快捷回复: 表情:
评论列表 (暂无评论,1632人围观)

还没有评论,来说两句吧...

目录[+]