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文件。

相关内容

热门资讯

透视科普!wepoker是不是... 透视科普!wepoker是不是有人用挂(透视)智星菠萝透视,教程绝活儿(有挂方略)-哔哩哔哩进入游戏...
透视详细!wepoker透视方... 透视详细!wepoker透视方法(透视)pokermaster破解版,教程指南书(有挂技巧)-哔哩哔...
透视了解!约局吧透视挂下载(透... 透视了解!约局吧透视挂下载(透视)hhpoker俱乐部是干嘛的,教程诀窍(真是有挂)-哔哩哔哩1、每...
透视解谜!pokemmo手机辅... 透视解谜!pokemmo手机辅助软件(透视)wpk俱乐部是做什么的,教程窍门(有挂教程)-哔哩哔哩1...
透视透视!wepoker私人局... 透视透视!wepoker私人局透视(透视)wepoker是不是有人用挂,教程技法(竟然有挂)-哔哩哔...
透视关于!拱趴大菠萝有挂吗(透... 透视关于!拱趴大菠萝有挂吗(透视)wepoker公共底牌,教程窍要(有挂方法)-哔哩哔哩1、拱趴大菠...
透视了解!wepoker能不能... 透视了解!wepoker能不能透视(透视)wepoker破解器有用吗,教程烘培(有挂秘笈)-哔哩哔哩...
透视总结!pokemmo辅助器... 透视总结!pokemmo辅助器脚本下载(透视)aapoker透视脚本,教程教程书(有挂总结)-哔哩哔...
透视开挂!hhpoker是内部... 透视开挂!hhpoker是内部控制吗(透视)we-poker靠谱吗,教程讲义(有挂方针)-哔哩哔哩1...
透视关于!来玩app 德州 辅... 透视关于!来玩app 德州 辅助(透视)hhpoker软件可以玩吗,教程学习(有挂教程)-哔哩哔哩1...