国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

[Angular] Export excel from table or json

這篇具有很好參考價(jià)值的文章主要介紹了[Angular] Export excel from table or json。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

How to implement?

1.NPM packages installation

npm i xlsx
npm i file-saver
npm i @types/file-saver

2.Create a service called?ExportExcel (export-excel.service.ts)

import { Injectable } from '@angular/core';

import * as fileSaver from 'file-saver';
import * as XLSX from 'xlsx';

@Injectable({
  providedIn: 'root'
})
export class ExportExcel {
  byJson(jsonData: any, fileName: string, header?: string[]): void {
    // create a worksheet
    const ws = XLSX.utils.json_to_sheet(jsonData, { header: header });
    // create a workbook
    let wb = XLSX.utils.book_new();
    // workbook append worksheet
    XLSX.utils.book_append_sheet(wb, ws, fileName);

    const data = XLSX.write(wb, { bookType: 'xlsx', type: 'array' });
    fileSaver.saveAs(new Blob([data], { type: 'application/octet-stream' }), `${fileName}.xlsx`);
  }

  byTable(table: any, fileName: string): void {
    const ws: XLSX.WorkSheet = XLSX.utils.table_to_sheet(table);
    const workbook: XLSX.WorkBook = XLSX.utils.book_new();
    XLSX.utils.book_append_sheet(workbook, ws, fileName);
    const excelBuffer: any = XLSX.write(workbook, {
      bookType: 'xlsx',
      type: 'array'
    });
    const data: Blob = new Blob([excelBuffer], {
      type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
    });
    fileSaver.saveAs(data, `${fileName}.xlsx`);
  }
}

3. There are two solutions to implement it.

a.?Solution 1: export Excel by Table

// Solution 1: export Excel by Table
  @ViewChild('basicTable', { read: ElementRef }) nzTable!: ElementRef;

  exportExcelByTable(): void {
    const fileName = 'exportExcelByTable';
    this.expExcel.byTable(this.nzTable.nativeElement, fileName);
    console.log(this.nzTable.nativeElement);
  }

b.?Solution 2: export Excel by Json

// Solution 2: export Excel by Json
  excelHeader = [
    {
      title: '#',
      fieldName: 'key'
    },
    {
      title: '姓名',
      fieldName: 'name'
    },
    {
      title: '年齡',
      fieldName: 'age'
    },
    {
      title: '地址',
      fieldName: 'address'
    }
  ];

  exportExcelByJson(): void {
    const fileName = 'exportExcelByJson';

    let jsonData: any;
    // listOfData from API
    jsonData = this.listOfData.map((row: any) => {
      let data: any = {};
      this.excelHeader.map((item: { fieldName: string; title: string }) => {
        data[item.title] = row[item.fieldName];
      });
      return data;
    });
    this.expExcel.byJson(jsonData, fileName);
  }

4. The full source code

import { NgFor } from '@angular/common';
import { ChangeDetectionStrategy, Component, ElementRef, OnInit, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';

import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzDividerModule } from 'ng-zorro-antd/divider';
import { NzInputModule } from 'ng-zorro-antd/input';
import { NzTableModule } from 'ng-zorro-antd/table';
import { ExportExcel } from 'src/custom/services/export-excel.service';

interface Person {
  key: string;
  name: string;
  age: number;
  address: string;
}

@Component({
  selector: 'app-pages-sample-export-excel',
  template: `
    <nz-table #basicTable [nzData]="listOfData">
      <thead>
        <tr>
          <th>Name</th>
          <th>Age</th>
          <th>Address</th>
          <th>Action</th>
        </tr>
      </thead>
      <tbody>
        <tr *ngFor="let data of basicTable.data">
          <td>{{ data.name }}</td>
          <td>{{ data.age }}</td>
          <td>{{ data.address }}</td>
          <td>
            <a>Action 一 {{ data.name }}</a>
            <nz-divider nzType="vertical"></nz-divider>
            <a>Delete</a>
          </td>
        </tr>
      </tbody>
    </nz-table>
    <button nz-button (click)="exportExcelByTable()">Export Excel By Table</button>
    <button nz-button (click)="exportExcelByJson()">Export Excel By Json</button>
  `,
  changeDetection: ChangeDetectionStrategy.OnPush,
  standalone: true,
  imports: [NzTableModule, NzDividerModule, NzButtonModule, NgFor, NzInputModule, FormsModule]
})
export class ExportExcelComponent implements OnInit {
  listOfData: Person[] = [
    {
      key: '1',
      name: 'John Brown',
      age: 32,
      address: 'New York No. 1 Lake Park'
    },
    {
      key: '2',
      name: 'Jim Green',
      age: 42,
      address: 'London No. 1 Lake Park'
    },
    {
      key: '3',
      name: 'Joe Black',
      age: 32,
      address: 'Sidney No. 1 Lake Park'
    }
  ];

  constructor(private expExcel: ExportExcel) {}

  ngOnInit(): void {}

  // Solution 1: export Excel by Table
  @ViewChild('basicTable', { read: ElementRef }) nzTable!: ElementRef;

  exportExcelByTable(): void {
    const fileName = 'exportExcelByTable';
    this.expExcel.byTable(this.nzTable.nativeElement, fileName);
    console.log(this.nzTable.nativeElement);
  }

  // Solution 2: export Excel by Json
  excelHeader = [
    {
      title: '#',
      fieldName: 'key'
    },
    {
      title: '姓名',
      fieldName: 'name'
    },
    {
      title: '年齡',
      fieldName: 'age'
    },
    {
      title: '地址',
      fieldName: 'address'
    }
  ];

  exportExcelByJson(): void {
    const fileName = 'exportExcelByJson';

    let jsonData: any;
    // listOfData from API
    jsonData = this.listOfData.map((row: any) => {
      let data: any = {};
      this.excelHeader.map((item: { fieldName: string; title: string }) => {
        data[item.title] = row[item.fieldName];
      });
      return data;
    });
    this.expExcel.byJson(jsonData, fileName);
  }
}

5. Test result

[Angular] Export excel from table or json,UI,json,前端,angular,經(jīng)驗(yàn)分享,excel,typescript

[Angular] Export excel from table or json,UI,json,前端,angular,經(jīng)驗(yàn)分享,excel,typescript文章來源地址http://www.zghlxwxcb.cn/news/detail-602977.html

到了這里,關(guān)于[Angular] Export excel from table or json的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包