Angular 2从getList()下载JSON响应数据并以CSV或Excel格式保存
创始人
2024-10-15 16:31:28
0

要实现从getList()下载JSON响应数据并以CSV或Excel格式保存,可以按照以下步骤进行:

  1. 创建一个Angular服务来处理数据下载和转换的逻辑。可以使用HttpClient来发送GET请求并获取JSON响应数据。在这个服务中,还需要使用第三方库来将JSON转换为CSV或Excel格式。
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { saveAs } from 'file-saver';
import * as XLSX from 'xlsx';

@Injectable()
export class DownloadService {
  constructor(private http: HttpClient) {}

  getList() {
    return this.http.get('your-api-url');
  }

  downloadCSV(data: any[], filename: string) {
    const csvContent = this.convertToCSV(data);
    this.saveToFile(csvContent, 'text/csv', filename);
  }

  downloadExcel(data: any[], filename: string) {
    const excelContent = this.convertToExcel(data);
    this.saveToFile(excelContent, 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', filename);
  }

  private convertToCSV(data: any[]) {
    // Convert JSON to CSV format
    const csvRows = [];
    const headers = Object.keys(data[0]);
    csvRows.push(headers.join(','));

    for (const row of data) {
      const values = headers.map(header => {
        const escaped = (''+row[header]).replace(/"/g, '\\"');
        return `"${escaped}"`;
      });
      csvRows.push(values.join(','));
    }

    return csvRows.join('\n');
  }

  private convertToExcel(data: any[]) {
    // Convert JSON to Excel format using XLSX library
    const worksheet = XLSX.utils.json_to_sheet(data);
    const workbook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
    const excelBuffer = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
    return excelBuffer;
  }

  private saveToFile(content: any, type: string, filename: string) {
    const blob = new Blob([content], { type: type });
    saveAs(blob, filename);
  }
}
  1. 在组件中使用该服务来获取数据并下载为CSV或Excel文件。
import { Component } from '@angular/core';
import { DownloadService } from './download.service';

@Component({
  selector: 'app-download',
  template: `
    
    
  `
})
export class DownloadComponent {
  constructor(private downloadService: DownloadService) {}

  downloadCSV() {
    this.downloadService.getList().subscribe(data => {
      this.downloadService.downloadCSV(data, 'data.csv');
    });
  }

  downloadExcel() {
    this.downloadService.getList().subscribe(data => {
      this.downloadService.downloadExcel(data, 'data.xlsx');
    });
  }
}
  1. 在模块中声明和提供服务。
import { NgModule } from '@angular/core';
import { HttpClientModule } from '@angular/common/http';
import { DownloadService } from './download.service';
import { DownloadComponent } from './download.component';

@NgModule({
  imports: [HttpClientModule],
  declarations: [DownloadComponent],
  providers: [DownloadService],
  exports: [DownloadComponent]
})
export class DownloadModule { }

现在,当用户点击"Download as CSV"按钮时,会将getList()的响应数据下载为CSV文件。点击"Download as Excel"按钮时,会将响应数据下载为Excel文件。

相关内容

热门资讯

每日必看推荐!werplan脚... 每日必看推荐!werplan脚本,hhpoker真能买到挂吗,科技教程(有挂透明)是一款可以让一直输...
热门推荐!wepokerplu... 热门推荐!wepokerplus万能挂,哈糖大菠萝挂,可靠技巧(有挂透视);1.wepokerplu...
揭秘关于!wepoker辅助脚... 此外,数据分析德州(hh poker辅助有用吗)辅助神器app还具备辅助透视行为开挂功能,通过对客户...
揭秘攻略!aapoker透视脚... 1、揭秘攻略!aapoker透视脚本下载,购买wepoker模拟器,存在挂教程(有挂透明)2、进入游...
必看攻略!wejoker辅助软... 必看攻略!wejoker辅助软件,wepoker能不能透视,2025版教程(有挂神器);原来确实真的...
玩家交流!xpoker辅助助手... 玩家交流!xpoker辅助助手,wepoker私人局俱乐部,技巧教程(有挂教程);《WPK辅助透视》...
重大通报!xpoker怎么作弊... 1、重大通报!xpoker怎么作弊,wepoker好友局透视,2025新版教程(有挂方法)。2、we...
科普攻略!wepoker透视最... 1、科普攻略!wepoker透视最简单三个步骤,德普之星私人局透视,玩家教程(有挂方法);详细教程。...
技巧知识分享!wepoker新... 技巧知识分享!wepoker新号好一点吗,wepokerplus辅助,科技教程(有挂辅助)科技教程也...
总算了解!aapoker透视怎... 总算了解!aapoker透视怎么用,约局吧能不能开挂,细节方法(有挂神器)是一款可以让一直输的玩家,...