text 将数组导出为ex​​cel

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了text 将数组导出为ex​​cel相关的知识,希望对你有一定的参考价值。

service:

npm install file-saver --save

bower install file-saver

Additionally, TypeScript definitions can be installed via:

npm install @types/file-saver --save-dev

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_TYPE = 'application/octet-stream';
const EXCEL_EXTENSION = '.xlsx';

@Injectable()
export class ExportExcelService {

  constructor() { }
  
  public exportAsExcelFile(json: any[], excelFileName: string): void {
    console.info(json);
    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);
  }
  
  private saveAsExcelFile(buffer: any, fileName: string): void {
    const data: Blob = new Blob([buffer], {
      type: EXCEL_TYPE
    });
    _filesaver.saveAs(data, fileName + '_export_' + new Date().getTime()+EXCEL_EXTENSION);
  }

}

以上是关于text 将数组导出为ex​​cel的主要内容,如果未能解决你的问题,请参考以下文章

如何在Laravel中将子数组元素导出为ex cel

csharp 将数据导出为ex​​cel

需要将XML文件数据导出为ex​​cel文件

角度材料数据表导出为ex cel

RadGrid导出为ex cel

SAS将数据集导出为csv或excel,保留换行符