首页 > 编程语言 >node-xlsx

node-xlsx

时间:2024-09-29 11:27:32浏览次数:5  
标签:node xlsx const buffer var data

node-xlsx

TypeScript icon, indicating that this package has built-in type declarations 0.24.0 • Public • Published 5 months ago

node-xlsx

npm version npm total downloads npm monthly downloads npm license build status

Features

Straightforward excel file parser and builder.

  • Relies on SheetJS xlsx module to parse/build excel sheets.
  • Built with TypeScript for static type checking with exported types along the library.

Install

npm install node-xlsx --save
# or
pnpm add node-xlsx

Quickstart

Parse an xlsx file

import xlsx from 'node-xlsx';
// Or var xlsx = require('node-xlsx').default;

// Parse a buffer
const workSheetsFromBuffer = xlsx.parse(fs.readFileSync(`${__dirname}/myFile.xlsx`));
// Parse a file
const workSheetsFromFile = xlsx.parse(`${__dirname}/myFile.xlsx`);

Build an xlsx file

import xlsx from 'node-xlsx';
// Or var xlsx = require('node-xlsx').default;

const data = [
  [1, 2, 3],
  [true, false, null, 'sheetjs'],
  ['foo', 'bar', new Date('2014-02-19T14:30Z'), '0.3'],
  ['baz', null, 'qux'],
];
var buffer = xlsx.build([{name: 'mySheetName', data: data}]); // Returns a buffer

Custom column width

import xlsx from 'node-xlsx';
// Or var xlsx = require('node-xlsx').default;

const data = [
  [1, 2, 3],
  [true, false, null, 'sheetjs'],
  ['foo', 'bar', new Date('2014-02-19T14:30Z'), '0.3'],
  ['baz', null, 'qux'],
];
const sheetOptions = {'!cols': [{wch: 6}, {wch: 7}, {wch: 10}, {wch: 20}]};

var buffer = xlsx.build([{name: 'mySheetName', data: data}], {sheetOptions}); // Returns a buffer

Spanning multiple rows A1:A4 in every sheets

import xlsx from 'node-xlsx';
// Or var xlsx = require('node-xlsx').default;

const data = [
  [1, 2, 3],
  [true, false, null, 'sheetjs'],
  ['foo', 'bar', new Date('2014-02-19T14:30Z'), '0.3'],
  ['baz', null, 'qux'],
];
const range = {s: {c: 0, r: 0}, e: {c: 0, r: 3}}; // A1:A4
const sheetOptions = {'!merges': [range]};

var buffer = xlsx.build([{name: 'mySheetName', data: data}], {sheetOptions}); // Returns a buffer

Spanning multiple rows A1:A4 in second sheet

import xlsx from 'node-xlsx';
// Or var xlsx = require('node-xlsx').default;

const dataSheet1 = [
  [1, 2, 3],
  [true, false, null, 'sheetjs'],
  ['foo', 'bar', new Date('2014-02-19T14:30Z'), '0.3'],
  ['baz', null, 'qux'],
];
const dataSheet2 = [
  [4, 5, 6],
  [7, 8, 9, 10],
  [11, 12, 13, 14],
  ['baz', null, 'qux'],
];
const range = {s: {c: 0, r: 0}, e: {c: 0, r: 3}}; // A1:A4
const sheetOptions = {'!merges': [range]};

var buffer = xlsx.build([
  {name: 'myFirstSheet', data: dataSheet1},
  {name: 'mySecondSheet', data: dataSheet2, options: sheetOptions},
]); // Returns a buffer

Beware that if you try to merge several times the same cell, your xlsx file will be seen as corrupted.

  • Using Primitive Object Notation Data values can also be specified in a non-abstracted representation.

Examples:

const rowAverage = [[{t: 'n', z: 10, f: '=AVERAGE(2:2)'}], [1, 2, 3]];
var buffer = xlsx.build([{name: 'Average Formula', data: rowAverage}]);

Refer to xlsx documentation for valid structure and values:

Troubleshooting

This library requires at least node.js v10. For legacy versions, you can use this workaround before using the lib.

npm i --save object-assign
Object.prototype.assign = require('object-assign');

Contributing

Please submit all pull requests the against master branch. If your unit test contains javascript patches or features, you should include relevant unit tests. Thanks!

Available scripts

ScriptDescription
start Alias of test:watch
test Run mocha unit tests
test:watch Run and watch mocha unit tests
lint Run eslint static tests
compile Compile the library
compile:watch Compile and watch the library

Authors

Olivier Louvignes

Copyright and license

Apache License 2.0

Copyright (C) 2012-2014  Olivier Louvignes

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

Except where noted, this license applies to any and all software programs and associated documentation files created by the Original Author and distributed with the Software:

Inspired by SheetJS gist examples, Copyright (c) SheetJS.

Readme

Keywords

标签:node,xlsx,const,buffer,var,data
From: https://www.cnblogs.com/sexintercourse/p/18439274

相关文章

  • 使用 Anchor 和 QuickNode 在 Solana 上创建NFT: 2024 版指南
    gg欢迎来到本教程。今天,我们将使用 SolanaPlayground、QuickNode RPC和一个IPFS服务,在Anchor/Rust中创建一个Solana程序,以直接在链上铸造NFT。作为预备步骤,我们将在去中心化存储服务中准备我们的NFT图像和元数据。我们将使用QuickNode IPFS,这是一个IPFS存储......
  • 基于nodejs+vue心里咨询与诊断平台系统[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着社会节奏的加快与生活压力的增大,心理健康问题日益凸显,成为影响公众生活质量的重要因素。传统心理咨询服务受限于地域、时间以及资源分配不均等问题,难以......
  • 基于nodejs+vue鞋类秒杀商城[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着互联网技术的飞速发展和电子商务的日益普及,线上购物已成为现代人不可或缺的生活方式之一。在鞋类消费领域,消费者对于时尚、品质与性价比的追求日益增强,......
  • 基于nodejs+vue携手助学助学交流平台[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景在当今社会,教育资源的不均衡分配问题日益凸显,尤其是在偏远地区及经济欠发达地区,优质教育资源的匮乏成为了制约学生成长与发展的关键因素。随着互联网技术的......
  • 基于nodejs+vue协同过滤音乐网站[开题+源码+程序+论文]计算机毕业设计
    本系统(程序+源码+数据库+调试部署+开发环境)带文档lw万字以上,文末可获取源码系统程序文件列表开题报告内容研究背景随着数字音乐产业的蓬勃发展,音乐网站已成为人们日常生活中不可或缺的一部分。然而,面对海量的音乐资源,如何高效、精准地为用户推荐符合其个人喜好的音乐成为......
  • 华为OD机试2024年E卷-转骰子[200分]( Java | Python3 | C++ | C语言 | JsNode | Go )实
    题目描述骰子是一个立方体,每个面一个数字,初始为左1,右2,前3(观察者方向),后4,上5,下6,用123456表示这个状态,放置在平面上,可以向左翻转(用L表示向左翻转1次),可以向右翻转(用R表示向右翻转1次),可以向前翻转(用F表示向前翻转1次),可以向后翻转(用B表示向后翻转1次),可以逆时针旋转(......
  • 华为OD机试2024年E卷-矩阵匹配[200分]( Java | Python3 | C++ | C语言 | JsNode | Go )
    题目描述从一个N*M(N≤M)的矩阵中选出N个数,任意两个数字不能在同一行或同一列,求选出来的N个数中第K大的数字的最小值是多少。输入描述输入矩阵要求:1≤K≤N≤M≤150输入格式:NMKN*M矩阵输出描述N*M的矩阵中可以选出M!/N!种组合数组,每个组合......
  • llama-factory挂载pm2出现问题:node: /lib64/libstdc++.so.6: version `CXXABI_1.3.9'
    使用ssh连接服务器上运行llama-factory进行微调,但是一旦关闭ssh,程序也会随之关闭,而使用nohup命令会出现nohup:ignoringinput尝试采用pm2:(base)[hongjiayin@localhostLLaMA-Factory]$pm2startstart.shnode:/lib64/libstdc++.so.6:version`CXXABI_1.3.9'notfound......
  • Node.js安装及详细配置(测试环境搭建)
    文章目录前言一、下载二、安装三、配置环境新建文件夹配置环境变量1.用户变量修改2.系统变量增加3.path路径增加修改默认文件夹测试默认位置是否更改成功报错解决方法npm源为更换国内镜像1.查看当前使用的镜像路径2.修改默认源全局安装基于淘宝源的cnpm1.cnpm安装意义......
  • prometheus学习笔记之node-export
    1.安装node-exportk8s各node节点使⽤⼆进制或者daemonset⽅式安装node_exporter,⽤于收集各k8snode节点宿主机的监控指标数据,默认监听端⼝为9100wgethttps://github.com/prometheus/node_exporter/releases/download/v1.3.1/node_exporter-1.3.1.linux-amd64.tar.gztarxfno......