how to export csv file in angularjs

Solutions on MaxInterview for how to export csv file in angularjs by the best coders in the world

showing results for - "how to export csv file in angularjs"
Tristan
13 Oct 2019
1 exportToCsv(filename: string, rows: object[]) {
2      if (!rows || !rows.length) {
3        return;
4      }
5      const separator = ',';
6      const keys = Object.keys(rows[0]);
7      const csvContent =
8        keys.join(separator) +
9        '\n' +
10        rows.map(row => {
11          return keys.map(k => {
12            let cell = row[k] === null || row[k] === undefined ? '' : row[k];
13            cell = cell instanceof Date
14              ? cell.toLocaleString()
15              : cell.toString().replace(/"/g, '""');
16            if (cell.search(/("|,|\n)/g) >= 0) {
17              cell = `"${cell}"`;
18            }
19            return cell;
20          }).join(separator);
21        }).join('\n');
22
23      const blob = new Blob([csvContent], { type: 'text/csv;charset=utf-8;' });
24      if (navigator.msSaveBlob) { // IE 10+
25        navigator.msSaveBlob(blob, filename);
26      } else {
27        const link = document.createElement('a');
28        if (link.download !== undefined) {
29          // Browsers that support HTML5 download attribute
30          const url = URL.createObjectURL(blob);
31          link.setAttribute('href', url);
32          link.setAttribute('download', filename);
33          link.style.visibility = 'hidden';
34          document.body.appendChild(link);
35          link.click();
36          document.body.removeChild(link);
37        }
38      }
39    }
Celian
18 Feb 2018
1alasql("SELECT * INTO CSV('mydata.csv', {headers:true}) FROM ?",[$scope.mydata]);
similar questions
queries leading to this page
how to export csv file in angularjs