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?

1

1 Answer 1

-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 
Sign up to request clarification or add additional context in comments.

5 Comments

What is Utils.makeTextFile?)
What is Utils.maketextFile ?? its undefined .
Function Added in Answer.
try to put the exact working code you used. Because I am pretty sure that is not it.
Added exact working code, removed the makeTextFile function, and writen all code in a single function.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.