zoukankan      html  css  js  c++  java
  • node读写Excel操作

    目支持写Excel的node.js模块:

    node-xlsx: 基于Node.js解析excel文件数据及生成excel文件;

    excel-parser: 基于Node.js解析excel文件数据,支持xls及xlsx格式文件;

    excel-export : 基于Node.js将数据生成导出excel文件,生成文件格式为xlsx;

    node-xlrd: 基于node.js从excel文件中提取数据,仅支持xls格式文件。

    下面通过node-xlsx模块来操作Excel文件。

     

    1、安装node-xlsx模块

    每次npm的时候,走国外的镜像,非常的慢,甚至安装失败,可以配置一下

    通过改变默认npm镜像代理服务,以下三种办法任意一种都能解决问题,建议使用第三种,将配置写死,下次用的时候不用重新配置。

    通过config命令

    npm config set registry https://registry.npm.taobao.org

    npm info underscore (如果上面配置正确这个命令会有字符串response)

    命令行指定

    npm --registry https://registry.npm.taobao.org info underscore

    编辑 ~/.npmrc 加入下面内容

    registry = https://registry.npm.taobao.org

     

    注:有时候执行npm会失败,提示

    Cannot find module 'C:Program Files odejs ode_modules pmin ode_modules pmin pm-cli.js'

    此时发现node_modules文件夹下npm文件加不存在,经常会出现这样的情况,于是我对此文件夹做个备份。

     

    安装node-xlsx

    npm install node-xlsx

    安装成功后,会在node_modules文件夹存在node-xlsx文件夹

    2、读写xlsx

     JavaScript Code 
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
     
    var xlsx = require('node-xlsx');
    var fs = require('fs');

    var data = [
        {
            name : 
    'sheet1',
            data : [
                [
                    
    'ID',
                    
    'Name',
                    
    'Score'
                ],
                [
                    
    '1',
                    
    'Michael',
                    
    '99'

                ],
                [
                    
    '2',
                    
    'Jordan',
                    
    '98'
                ]
            ]
        },
        {
            name : 
    'sheet2',
            data : [
                [
                    
    'AA',
                    
    'BB'
                ],
                [
                    
    '23',
                    
    '24'
                ]
            ]
        }
    ]

    // 写xlsx
    var buffer = xlsx.build(data);
    fs.writeFile(
    './resut.xls', buffer, function (err)
    {
        
    if (err)
            
    throw err;
        console.log(
    'Write to xls has finished');
        
    // 读xlsx
        var obj = xlsx.parse("./" + "resut.xls");
        console.log(JSON.stringify(obj));
    }
    );

    注意:文件扩展名可以是xls,也可以是xlsx

    运行后输出:

    附录:使用nodemonnode自动重启

    在我们开发node项目的时候,修改了服务的代码,但是node并自动重启来生效,必须手动去重启一下,这样很烦人的啊,很浪费时间,于是就有大神开发了自动重启的工具——nodemon,很简单的。

    1. npm install -g nodemon  

    此时就可以用nodemon来代替node命令,执行服务器端js脚本了~

     

  • 相关阅读:
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    Security and Cryptography in Python
    基于分布式锁解决定时任务重复问题
    基于Redis的Setnx实现分布式锁
    基于数据库悲观锁的分布式锁
    使用锁解决电商中的超卖
  • 原文地址:https://www.cnblogs.com/MakeView660/p/7902715.html
Copyright © 2011-2022 走看看