将数据从Angular导出到Excel开关列

时间:2019-05-16 12:39:50

标签: excel angular typescript

我正在尝试将数据从angular导出到excel,我在后端使用JEE,现在我没有问题,一切正常。但是我得到的EXCEL文件却给了我倒置的列。

这是component.html

<table class="table table-striped">
  <thead>
    <tr>
      <th scope="col" sortable="name" (sort)="onSort($event)">Type</th>
      <th>Entité</th>
      <th>Modifié par</th>
      <th>Date de modification</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let x of respTest | paginate: {itemsPerPage: 10, currentPage:page, id: '1'}; let i = index">
      <td>{{x.Type}}</td>
      <td>{{x.Entite}}</td>
      <td>{{x.ModifiePar}}</td>
      <td>{{x.DateModif}}</td>
    </tr>
  </tbody>
</table>
<button type="button" class="btn btn-primary" (click)="exportAsXLSX()">Export</button>

这是我在component.ts中使用的功能

 exportAsXLSX():void {
     this.excelService.exportAsExcelFile(this.respTest, 'suivi');
 }

使用此服务excel.service.ts

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 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 + '_export_' + new Date().getDate() + EXCEL_EXTENSION);
  }

}

我希望列的顺序与前面显示的表格中的顺序相同。

0 个答案:

没有答案