博客
关于我
nodejs 读取xlsx文件内容
阅读量:793 次
发布时间: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/

你可能感兴趣的文章
No fallbackFactory instance of type class com.ruoyi---SpringCloud Alibaba_若依微服务框架改造---工作笔记005
查看>>
No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalanc
查看>>
No mapping found for HTTP request with URI [/...] in DispatcherServlet with name ...的解决方法
查看>>
No mapping found for HTTP request with URI [/logout.do] in DispatcherServlet with name 'springmvc'
查看>>
No module named 'crispy_forms'等使用pycharm开发
查看>>
No module named cv2
查看>>
No module named tensorboard.main在安装tensorboardX的时候遇到的问题
查看>>
No module named ‘MySQLdb‘错误解决No module named ‘MySQLdb‘错误解决
查看>>
No new migrations found. Your system is up-to-date.
查看>>
No qualifying bean of type XXX found for dependency XXX.
查看>>
No qualifying bean of type ‘com.netflix.discovery.AbstractDiscoveryClientOptionalArgs<?>‘ available
查看>>
No resource identifier found for attribute 'srcCompat' in package的解决办法
查看>>
no session found for current thread
查看>>
No toolchains found in the NDK toolchains folder for ABI with prefix: mips64el-linux-android
查看>>
NO.23 ZenTaoPHP目录结构
查看>>
no1
查看>>
NO32 网络层次及OSI7层模型--TCP三次握手四次断开--子网划分
查看>>
NOAA(美国海洋和大气管理局)气象数据获取与POI点数据获取
查看>>
NoClassDefFoundError: org/springframework/boot/context/properties/ConfigurationBeanFactoryMetadata
查看>>
node exporter完整版
查看>>