Is there a way to download a csv file of the data from a SQL Server database table on button click in Angular

前端 未结 2 336
失恋的感觉
失恋的感觉 2021-01-16 17:02

I need to get all of the data in my SQL Server database table into a .csv file when I click a button in a front end Angular web page.

I have already wr

2条回答
  •  悲&欢浪女
    2021-01-16 17:36

    You can use the package, file-saver for downloading files from blob. Load the data and generate a CSV string which can be converted into a blob object.

    npm i file-saver

    npm i @types/file-saver

    app.component.ts :

    import { saveAs } from 'file-saver'
    
    download(){
        // Variable to store data as CSV string 
        let csvStr = '';
        // Fetch data from service
        this.employeeService.getAllEmployee().subscribe(
            response => { 
                // Manipulate data to generate CSV string
            },error => {}
        );
       
        // Convert string to blob
        var csvBlob = new Blob([csvStr], {
          type: 'text/plain'
        });
        // Download
        saveAs(csvBlob,'data.csv')
    }
    

    Demo : https://stackblitz.com/edit/angular-zhqbgp

提交回复
热议问题