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,467
  • 1
  • 21
  • 31
  • 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.

Below is my javascript function for download csv

downloadCSV(jsonData){
    const arr = typeof jsonData !== 'object' ? JSON.parse(jsonData) : jsonData;
    const str = `${Object.keys(arr[0]).map((value) => `${value}`).join(',')}\r\n`;
    const csvContent = arr.reduce((st, next) => {
      st += `${Object.values(next).map((value) => 
        `${value}`).join(',')}\r\n`;
         return st;
      }, str);
    const element = document.createElement('a');
    element.href = `data:text/csv;charset=utf8,${encodeURI(csvContent)}`;
    element.target = '_blank';
    element.download = 'export.csv';
    element.click();    }

value of data is in CSV format data

col1, col2, col3
row1-value1, row1-value2, row1-value3
Maulik Kakadiya
  • 1,467
  • 1
  • 21
  • 31