博客
关于我
nodejs 读取xlsx文件内容
阅读量:792 次
发布时间:2023-02-16

本文共 1510 字,大约阅读时间需要 5 分钟。

Node.js 读取 XLSX 文件内容与处理

1. 安装依赖

首先,需要通过 npm 安装 node-xlsx 库:

npm install node-xlsx --save-dev

2. 读取 Excel 文件

使用 node-xlsx 库读取 Excel 文件,可以按照以下步骤操作:

const xlsx = require('node-xlsx');const sheets = xlsx.parse('F:\\123.xlsx');

注意:确保文件路径正确,建议使用绝对路径。

3. 获取 Excel 表格数据

读取完成后,sheets 会是一个数组,包含所有工作表。具体内容可以通过遍历 sheets 来查看。

4. 处理日期格式

Excel 中的日期格式在 Node.js 中可能会以特定方式存储,下面提供一个将日期格式转换为 Date 对象的方法:

const getFormatDate_XLSX = (serial: number) => {    const utc_days = Math.floor(serial - 25569);    const utc_value = utc_days * 86400;    const date_info = new Date(utc_value * 1000);    const fractional_day = serial - Math.floor(serial) + 0.0000001;    const total_seconds = Math.floor(86400 * fractional_day);    const seconds = total_seconds % 60;    const total_seconds -= seconds;    const hours = Math.floor(total_seconds / (60 * 60));    const minutes = Math.floor(total_seconds / 60) % 60;    const d = new Date(        date_info.getFullYear(),        date_info.getMonth(),        date_info.getDate(),        hours,        minutes,        seconds    );    const add0 = (m: number) => m < 10 ? '0' + m : m.toString();    return `${add0(d.getFullYear())}-${add0(d.getMonth() + 1)}-${add0(d.getDate())} ${add0(d.getHours())}:${add0(d.getMinutes())}:${add0(d.getSeconds())}`;};

5. 使用示例

完整的使用示例代码如下:

const xlsx = require('node-xlsx');const sheets = xlsx.parse('F:\\123.xlsx');console.log('内容:', sheets);

6. 其他注意事项

  • 确保 Excel 文件已正确保存,且格式正确。
  • 日期转换方法可根据实际需求进行调整。
  • 使用绝对路径时,请确保路径正确无误。

7. 结论

通过以上方法,可以轻松读取并处理 XLSX 文件内容。希望以上内容对您有所帮助!

转载地址:http://ftjfk.baihongyu.com/

你可能感兴趣的文章
NLP:使用 SciKit Learn 的文本矢量化方法
查看>>
nmap 使用方法详细介绍
查看>>
nmap使用
查看>>
Nmap扫描教程之Nmap基础知识
查看>>
nmap指纹识别要点以及又快又准之方法
查看>>
Nmap渗透测试指南之指纹识别与探测、伺机而动
查看>>
Nmap端口扫描工具Windows安装和命令大全(非常详细)零基础入门到精通,收藏这篇就够了
查看>>
NMAP网络扫描工具的安装与使用
查看>>
NMF(非负矩阵分解)
查看>>
nmon_x86_64_centos7工具如何使用
查看>>
NN&DL4.1 Deep L-layer neural network简介
查看>>
NN&DL4.3 Getting your matrix dimensions right
查看>>
NN&DL4.7 Parameters vs Hyperparameters
查看>>
NN&DL4.8 What does this have to do with the brain?
查看>>
nnU-Net 终极指南
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
No 'Access-Control-Allow-Origin' header is present on the requested resource.
查看>>
NO 157 去掉禅道访问地址中的zentao
查看>>
no available service ‘default‘ found, please make sure registry config corre seata
查看>>
No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK?
查看>>