node.js 笔记

正则多字符匹配

  var re = /最大|绝对|最低|第一/gi;
                var resname = name.replace(re, function (sMatch) {
                    return sMatch.replace(/./g, "");
                });

fs写入读取

const loadData = (path: any) => {
    try {
        return fs.readFileSync(path, 'utf8')
    } catch (err) {
        console.error(err)
        return false
    }
}


const storeData = (data: any[], path: string) => {
    try {
        fs.writeFileSync(path, JSON.stringify(data))
    } catch (err) {
        console.error(err)
    }
}

const WriteError = (data: string, path: string) => {
    try {
        fs.appendFileSync(path, data)
    } catch (err) {
        console.error(err)
    }
}

导出Excel

var xlsx = require('node-xlsx');
var fs = require('fs');

var data1 = [
    {
        name: 'sheet1', data: [
            ['ID', 'Name', 'Score'],
            [1, 'Michael', 99],
            [2, 'Jordan', 98]]
    },
    {
        name: 'sheet2', data: [
            ['ID', 'Name', 'Score'],
            [1, 'Michael', 99],
            [2, 'Jordan', 98]]
    }
]
data1[0].data.push([
    123123,
    'Jordan',
    123
]);

var buffer = xlsx.build(data1);

fs.writeFile('excelfile.xlsx', buffer, function (err: any) {
    if (err) {
        throw err
    }
    console.log('生成成功')
})

 

posted @ 2020-12-16 15:14  shenghuotaiai  阅读(74)  评论(0编辑  收藏  举报