Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

angular array export to excel

//DOWNLOAD
  download(){
    var csvData = this.ConvertToCSV( this.data);
    var a = document.createElement("a");
    a.setAttribute('style', 'display:none;');
    document.body.appendChild(a);
    var blob = new Blob([csvData], { type: 'text/csv' });
    var url= window.URL.createObjectURL(blob);
    a.href = url;
    var x:Date = new Date();
    var link:string ="filename_" + x.getMonth() +  "_" +  x.getDay() + '.csv';
    a.download = link.toLocaleLowerCase();
    a.click();

  }


// convert Json to CSV data in Angular2
  ConvertToCSV(objArray) {
    var array = typeof objArray != 'object' ? JSON.parse(objArray) : objArray;
    var str = '';
    var row = "";

    for (var index in objArray[0]) {
        //Now convert each value to string and comma-separated
        row += index + ',';
    }
    row = row.slice(0, -1);
    //append Label row with line break
    str += row + '
';

    for (var i = 0; i < array.length; i++) {
        var line = '';
        for (var index in array[i]) {
            if (line != '') line += ','

            line += array[i][index];
        }
        str += line + '
';
    }
    return str;
  }
Comment

export excel form angular array to excel

import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';

const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';
@Injectable()
export class ExcelService {

constructor() { }

public exportAsExcelFile(json: any[], excelFileName: string): void {

    const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);

    const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
    const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
    this.saveAsExcelFile(excelBuffer, excelFileName);
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: lodash isempty 
Javascript :: Make Floating label TextInput in react native 
Javascript :: destructuring assignment 
Javascript :: sequelize get all data 
Javascript :: js check if a string is a number 
Javascript :: Making promises 
Javascript :: function that duplicates data in array js 
Javascript :: js max array 
Javascript :: Using the reverse method to Reverse an Array 
Javascript :: node.js brotli 
Javascript :: cookies in react native 
Javascript :: check if array exists in another array javascript 
Javascript :: array some 
Javascript :: mongoose update 
Javascript :: how to write a variable in js 
Javascript :: Javascript Map properties and methods 
Javascript :: ./node_modules/bootstrap/dist/js/bootstrap.bundle.js 
Javascript :: js ignore case 
Javascript :: calculate two number and diplay next field without reload the page javascript 
Javascript :: jquery slider value 
Javascript :: on hover display block jquery 
Javascript :: use react awesome slider in react js 
Javascript :: chess game in javascript github 
Javascript :: vue router transition 
Javascript :: Correct regex for extracting URl 
Javascript :: react class components 
Javascript :: react input cursor jump end 
Javascript :: js session storage 
Javascript :: add parameter at the end of url from jquery with refreshing 
Javascript :: dot geometru three js 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =