nodejs fs.writeFile()创建具有相同内容的多个文件

时间:2017-06-28 07:42:07

标签: node.js fs

我正在尝试通过用户名将数据从xlsx工作表拆分为多个docx文件。首先,我使用node-excel-to-json将excel中的数据转换为json格式。然后我使用docxtemplater将数据以json格式放入新创建的docx文件中。生成的第一个文件的内容是正确的,但是从第二个文件到最后一个文件的内容与第一个文件相同。它看起来像这样:

sample.xlsx:

姓名phonenumber

john 123456

john 654321

jack 987654

迈克999999

input.docx(作为模板):{#record} {name} {phonenumber} {/ record}

生成docx:

john.docx:john 123456 john 654321

jack.docx:john 123456 john 654321

mike.docx:john 123456 john 654321

这是我的代码

var excel2Json = require('node-excel-to-json');
var JSZip = require('jszip');
var Docxtemplater = require('docxtemplater');
var fs = require('fs');
var path = require('path');

var content = fs.readFileSync(path.resolve(__dirname, 'input.docx'), 'binary');
var zip = new JSZip(content);
var doc = new Docxtemplater();
doc.loadZip(zip);
excel2Json('../../../sample.xlsx', {
    'convert_all_sheet': false,
    'return_type': 'Object',
    'sheetName': 'phonenumber'
}, function(err, output) {
    var i = 0;
    var buf = new Array();
    var nowPerson = {
     name : output[1].author,
     data : {"record": []}
     }
    for(var singleRecord in output) {
        console.log(output[singleRecord].author);
        if((output[singleRecord].author===nowPerson.name)){
            //console.log(output[singleRecord]);
            nowPerson.data.record.push(output[singleRecord]);
        }
        else{
            console.log(nowPerson.data);
            doc.setData(nowPerson.data);
            try {
                doc.render();
            }
            catch (error) {
                var e = {
                    message: error.message,
                    name: error.name,
                    stack: error.stack,
                    properties: error.properties,
                }
                console.log(JSON.stringify({error: e}));
                // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
                throw error;
            }
            buf = doc.getZip()
                .generate({type: 'nodebuffer'});
            fs.writeFileSync(path.resolve(__dirname, nowPerson.name+'.docx'), buf);
            nowPerson.name = output[singleRecord].author;
            nowPerson.data.record = [];
            nowPerson.data.record.push(output[singleRecord]);
        }
    }
    doc.setData(nowPerson.data);
    try {
        doc.render()
    }
    catch (error) {
        var e = {
            message: error.message,
            name: error.name,
            stack: error.stack,
            properties: error.properties,
        }
        console.log(JSON.stringify({error: e}));
        // The error thrown here contains additional information when logged with JSON.stringify (it contains a property object).
        throw error;
    }
    var buf = doc.getZip()
        .generate({type: 'nodebuffer'});
    fs.writeFileSync(path.resolve(__dirname, nowPerson.name+'.docx'), buf);

});

我尝试了fs.writeFile()fs.writeFileSync(),但两者都输出了与上面相同的结果。谢谢你的帮助!

0 个答案:

没有答案