嗨,我需要在angular5中导出表的数据,我正在使用API xls并将文件保存为我的代码:
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_EXTENSION = '.xlsx';
@Injectable()
export class ExportexcelService {
constructor() { }
public exportAsExcelFile(json: any[], excelFileName: string): void {
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});
var today = new Date();
var date = today.getFullYear() + '' + (today.getMonth() + 1) + '' +
today.getDate() + '_';
var time = today.getHours() + "-" + today.getMinutes() + "-" +
today.getSeconds();
var name = fileName + date + time;
FileSaver.saveAs(data, name + EXCEL_EXTENSION);
}
}
表中的数据包含日期,货币行,此代码有效,但输出的日期和数字格式不正确。
注意:数据的代码采用json格式,我称之为data:Interfacename [];。该接口仅包含我的变量的名称,例如: