2

I have tried to create CSV file in vue.js. server send readyment data which is we can directly write data in csv file. data from server response is

head1, head2, head3, head4
1,2,3,4
5,6,7,8

This is my response formate from the server. I want to write this data in CSV format and give download CSV functionality.

Any Idea about create and write a file in vue.js?

Maulik Kakadiya
  • 1,246
  • 1
  • 17
  • 28
  • Possible duplicate of [Javascript to export html table to Excel](https://stackoverflow.com/questions/17142427/javascript-to-export-html-table-to-excel) – Dbercules Nov 23 '18 at 10:00

1 Answers1

-1

I got my answer by using javascript.

I have create one hidden html <a> tag

<a href="" id="downloadlink" ref="csvfiledownload" hidden="true" download="mycsv.csv">download</a>

Bellow is my javascript function for download csv

downloadCSV(data){
    var link = document.getElementById('downloadlink');
    link.href = Utils.makeTextFile(response.data);
    link.style.display = 'block';
    link.click();
}

value of data is in CSV format data

col1, col2, col3
row1-value1, row1-value2, row1-value3

here my makeTextFile function.

function makeTextFile {
    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "mycsv.csv");
    document.body.appendChild(link);
    return link;

}
Maulik Kakadiya
  • 1,246
  • 1
  • 17
  • 28