npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

iver-excel

v1.0.4

Published

提供一种用nodejs操作EXCEL文件的简单方式

Downloads

1

Readme

iver-excel

一个轻量级的模块,如果你的应用场景是从数据库里得到一个集合,然后需要把集合按照一定的规则写入到excel里面,或者准备从excel里把数据导入到数据库,那么…… 用她,用她。

Install

npm install --save iver-excel

Method

  • SaveExcel - 将数据保存到excel。
  • CreateExcelData - 格式化对象数组。
  • SaveTable - 将对象数组保存为excel文件。
  • ReadExcel - 读取excel。
方法的参数,可以参考下面的使用。

Usage

将数据保存到excel-方式一:data参数为对象数组

const { SaveExcel } = require("iver-excel");
const path = require("path");
const result1 = SaveExcel({
  //创建的文件路径
  pathName: path.join(__dirname, `./jsonData.xlsx`),
  sheetArr: [
    {
      //sheet名
      sheetName: "data",
      //对象数组
      data: [
        { name: "小明", sex: "男", age: 16 },
        { name: "小红", sex: "女", age: 18 },
      ]
    },
    //创建多个sheet
    //...
  ],
})

将数据保存到excel-方式二:data参数为数组

const { SaveExcel } = require("iver-excel");
const path = require("path");
const result2 = SaveExcel({
  //创建的文件路径
  pathName: path.join(__dirname, `./arrData.xlsx`),
  sheetArr: [
    {
      //sheet名
      sheetName: "data",
      //数组
      data: [
        ["A", "B", "C"],
        ["D", "E", "F", "G"],
        ["H", "I", "J", "K", "L"],
      ]
    },
    //创建多个sheet
    //...
  ],
})

也可在调用SaveExcel方法的时候,利用CreateExcelData方法给data参数加工处理一下,注意CreateExcelData仅仅适用于对象数组,也就是方法一 在excel中我们往往不希望字段显示是英文,所以有了mapping参数

const { SaveExcel, CreateExcelData } = require("iver-excel");
const path = require("path");
const result3 = SaveExcel({
  //创建的文件路径
  pathName: path.join(__dirname, `./formartData.xlsx`),
  sheetArr: [
    {
      //sheet名
      sheetName: "data",
      //对象数组
      data: CreateExcelData({
        arr: [
          { name: "小明", sex: "男", age: 16 },
          { name: "小红", sex: "女", age: 18 },
        ],
        //是否有序号
        isOrder: true,
        //可以调换顺序,来调整excel最终输出的顺序,比如这里调换了名字和年龄;也可以不写对应关系来去掉某个字段,比如只写名字和性别
        mapping: {
          name: "姓名",
          age: "年龄",
          sex: "性别",
        }
      })
    },
    //创建多个sheet
    //...
  ],
})

我们的应用场景,往往是从一个数据库或者某个地方得到一个数据集合,然后按照一定的规则导入到excel里面去,所有就有了SaveTable方法。 SaveTable方法实际上是把上述方法结合了一下,也是我最常用拿来用的。

const { SaveTable } = require("iver-excel");
const path = require("path");
const result4 = SaveTable({
  arr: [
    { name: "小明", sex: "男", age: 16 },
    { name: "小红", sex: "女", age: 18 },
  ],
  mapping: {
    name: "姓名",
    age: "年龄",
    sex: "性别",
  },
  isOrder: true,
  pathName: path.join(__dirname, `./Data.xlsx`),
})

读取excel文件

const { ReadExcel } = require("iver-excel");
const path = require("path");
const result5 = ReadExcel({
  //文件路径
  filePath : path.join(__dirname, `./jsonData.xlsx`),
  //sheet-index 读取哪个sheet,索引从0开始
  sheetIndex : 0, 
  //raw true:按照默认格式输出 false:按照字符串格式输出 默认为true
})

More

在实际应用中,我们往往需要把数据库的某张表,导入到excel文件里去;或者的把excel文件里的数据导入到数据库中去。iver-excel 处理了导入导出中的一些小细节,应该能满足你的大部分需求。


如果你是在sqlServer和Excel之间操作 那么我只能说,缘分啊。可以参考我的另一个模块 iver-mssql。其实数据源是什么都不重要,只要最终能转成对象数组或者json数组,都能使用 iver-excel 完成任务。