admin管理员组

文章数量:1320652

I am able to write data into an xls sheet but not in a proper mannaer ,pls see the pic below

My xls sheet

My code,

var readString = '';
      readString = "Name"+"\t"+" From"+"\t"+" Created By"+"\t"+" Reminder Date"+"\t"+" Reminder Time"+"\n";
      event.forEach(function (v, i) {
        readString += v.attributes.task_name+"\t";
        readString += v.attributes.context_type+"\t";
        readString += v.created_by.employee.name+"\t";
        readString += v.attributes.reminder_date+"\t";
        readString += v.attributes.reminder_time+"\t";
        readString += "\n";
      });
      var currTime = new Date().getTime();
      var filePath = "./modules/upload/excel/task_" + currTime + ".xls";
      //studentInfo.filePath = "task_" + currTime + ".xls";
      var writeStream = fs.createWriteStream(filePath);
      writeStream.write(readString);
      writeStream.close();

Can anyone please help me.Thanks.

I am able to write data into an xls sheet but not in a proper mannaer ,pls see the pic below

My xls sheet

My code,

var readString = '';
      readString = "Name"+"\t"+" From"+"\t"+" Created By"+"\t"+" Reminder Date"+"\t"+" Reminder Time"+"\n";
      event.forEach(function (v, i) {
        readString += v.attributes.task_name+"\t";
        readString += v.attributes.context_type+"\t";
        readString += v.created_by.employee.name+"\t";
        readString += v.attributes.reminder_date+"\t";
        readString += v.attributes.reminder_time+"\t";
        readString += "\n";
      });
      var currTime = new Date().getTime();
      var filePath = "./modules/upload/excel/task_" + currTime + ".xls";
      //studentInfo.filePath = "task_" + currTime + ".xls";
      var writeStream = fs.createWriteStream(filePath);
      writeStream.write(readString);
      writeStream.close();

Can anyone please help me.Thanks.

Share Improve this question asked Feb 8, 2017 at 12:40 strikerstriker 511 gold badge2 silver badges5 bronze badges 3
  • Why not use xlsx npm package? – Maher Fattouh Commented Feb 8, 2017 at 12:49
  • What you are creating is a TSV (tab-separated values) file. If you want to write xls, xlsx use a node module to do it. E.g. json2xls, node-xlsx, xlsx – Sangharsh Commented Feb 8, 2017 at 12:50
  • You're writing it as a text. Excel files have a different header, style and formating info and many more things that you need to take care of. The easiest way is to use a node module that take care of all of that. Just search in npm website – Maher Fattouh Commented Feb 8, 2017 at 12:52
Add a ment  | 

1 Answer 1

Reset to default 4

To work with XLS files, see:

  • https://www.npmjs./package/node-xls
  • https://www.npmjs./package/xlsx
  • https://www.npmjs./browse/keyword/xls

But looking at your code you seem to be working with TSV data and not XLS. See:

  • https://www.npmjs./package/csv
  • https://www.npmjs./package/tsv
  • https://www.npmjs./browse/keyword/csv
  • https://www.npmjs./browse/keyword/tsv

Note that TSV is the same as CSV with the only difference being the delimiter.

本文标签: javascriptHow to write data into an xls sheet in jsStack Overflow