我使用单独的excel服务并在ts文件中定义了函数,在我的前端角度中添加了export to excel选项。这工作正常,但在Excel工作表中,我将为_id和__V字段获得2个附加列。我无法删除这两个字段。我给了下面的代码。任何人都可以在这方面提供帮助
excel service.ts
import { Injectable } from '@angular/core';
import * as FileSaver from 'file-saver';
import * as XLSX from 'xlsx';
import * as moment from 'moment'
const EXCEL_TYPE = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8';
const EXCEL_EXTENSION = '.xlsx';
@Injectable({
providedIn: 'root'
})
export class ExcelService {
constructor() { }
public exportAsExcelFile(json: any[], excelFileName: string): void {
const worksheet: XLSX.WorkSheet = XLSX.utils.json_to_sheet(json);
console.log('worksheet', worksheet);
const workbook: XLSX.WorkBook = { Sheets: { 'data': worksheet }, SheetNames: ['data'] };
const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'array' });
//const excelBuffer: any = XLSX.write(workbook, { bookType: 'xlsx', type: 'buffer' });
this.saveAsExcelFile(excelBuffer, excelFileName);
}
private saveAsExcelFile(buffer: any, fileName: string): void {
const data: Blob = new Blob([buffer], {
type: EXCEL_TYPE
});
FileSaver.saveAs(data, fileName + '_db_' + moment().format('MM-DD-YY_h.mm a') + EXCEL_EXTENSION);
}
}
html
<button (click)="exportAsXLSX()" class="btn btn-success">Export to Excel</button>
ts文件
exportAsXLSX():void {
this.excelService.exportAsExcelFile(this.VILKerala.data,'VILKerala');
}}
export interface VILKerala {}