1

I am trying to export datatable to excel in javascript. I am using d3.js , dc.js and crossfilter.js . My code is based off of this example.

FileSaver.js is used to export Datatable.

I want to export my datatable to excel (instead of CSV). How do I do that?

   d3.select('#btnExport')
        .on('click', function() {
            var data = newTableDim.top(Infinity);
            var blob = new Blob([d3.csv.format(data)], {type: "text/csv;charset=utf-8"});
            saveAs(blob, 'data.csv');
        });

How do I change it to save results to excel ? Any Pointers?

Ref: Export Data from dc.js dataTable into CSV

EDIT: I get this Excel Warning, after I save it as xls.

enter image description here

I guess I can keep it as .csv.

ProgSky
  • 2,530
  • 8
  • 39
  • 65
  • 1
    Excel is fully compatible with CSV - in fact, I think they invented the format way back. Is the issue making sure that Excel opens the file instead of some other application? I wouldn't recommend it but I've sometimes seen people name their CSVs .XLS and Excel happily opened them. :-p – Gordon Oct 14 '17 at 00:42
  • @Gordon . Thanks for response. I do get a warning message when I rename (I have added that to my question). Excel does open the file ok. I was hoping to avoid it. – ProgSky Oct 16 '17 at 14:21
  • 1
    I agree, it's a bad idea to lie about the format. I guess if .csv won't work for you, you could try a library like [js-xlsx](https://www.npmjs.com/package/xlsx) to produce real XLS files. I've never tried it myself... – Gordon Oct 16 '17 at 14:29
  • thanks. I will give it a go. – ProgSky Oct 16 '17 at 17:11

0 Answers0