Angular Grid Excel を Excel サービスへエクスポート
Excel Exporter サービスは IgxGrid のデータを MS Excel へエクスポートできます。エクスポート機能は、IgxExcelExporterService
クラスでカプセル化され、MS Excel テーブル形式でデータをエクスポートします。この形式ではフィルタリングやソートなどの機能が使用でき、IgxExcelExporterService
の export
メソッドを呼び出して最初の引数として IgxGrid コンポーネントを渡し、グリッドを簡単にエクスポートします。
Angular Excel Exporter の例
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import {
IgxGridModule,
IgxExcelExporterService,
IgxButtonModule
} from "igniteui-angular";
import { ExcelExportSample1Component } from "./services/excel-export-sample-1/excel-export-sample-1.component";
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
ExcelExportSample1Component
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxGridModule,
IgxButtonModule
],
providers: [IgxExcelExporterService],
entryComponents: [],
schemas: []
})
export class AppModule {}
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import {
GridColumnDataType,
IgxExcelExporterService,
IgxGridComponent,
ISortingExpression,
SortingDirection
} from 'igniteui-angular';
import { INVOICE_DATA } from '../data/invoiceData';
@Component({
selector: 'app-excel-export-sample-1',
styleUrls: ['./excel-export-sample-1.component.scss'],
templateUrl: './excel-export-sample-1.component.html'
})
export class ExcelExportSample1Component implements OnInit {
@ViewChild('igxGrid1', { static: true }) public igxGrid1: IgxGridComponent;
public data = [];
public groupExpressions: ISortingExpression[];
public columns: { dataType: GridColumnDataType, field: string, width: string, groupable: boolean, hidden?: boolean }[] = [
{ dataType: GridColumnDataType.String, field: 'ShipCountry', width: '150', groupable: true },
{ dataType: GridColumnDataType.String, field: 'ShipCity', width: '150', groupable: true },
{ dataType: GridColumnDataType.String, field: 'ShipAddress', width: '150', groupable: true},
{ dataType: GridColumnDataType.String, field: 'PostalCode', width: '150', groupable: true },
{ dataType: GridColumnDataType.Date, field: 'OrderDate', width: '150', groupable: true },
{ dataType: GridColumnDataType.Number, field: 'Quantity', width: '150', groupable: true }
];
constructor(private excelExportService: IgxExcelExporterService) {
}
public ngOnInit(): void {
this.data = INVOICE_DATA;
this.groupExpressions = [
{ dir: SortingDirection.Asc, fieldName: 'ShipCountry' },
{ dir: SortingDirection.Asc, fieldName: 'ShipCity' }
];
}
}
ts
<p class="grid__wrapper">
<igx-grid #igxGrid1 [data]="data" [groupingExpressions]="groupExpressions" [allowFiltering]="true" [cellSelection]="'single'"
[hideGroupedColumns]="true" [width]="'100%'" [height]="'700px'" [moving]="true">
<igx-grid-toolbar>
<igx-grid-toolbar-actions>
<igx-grid-toolbar-exporter [exportExcel]="true" [exportCSV]="false">
</igx-grid-toolbar-exporter>
</igx-grid-toolbar-actions>
</igx-grid-toolbar>
<igx-column *ngFor="let c of columns" [sortable]="true" [field]="c.field" [header]="c.field" [width]="c.width"
[hidden]="c.hidden" [groupable]="c.groupable" [editable]="true" [dataType]="c.dataType">
</igx-column>
</igx-grid>
</p>
html
.grid__wrapper {
padding-top: 16px;
width: 98% !important;
margin: 0 auto;
padding-left: 1%;
padding-right: 1%;
}
.exportButton {
margin-top: 5px;
}
scss
このサンプルが気に入りましたか? 完全な Ignite UI for Angularツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
Grid のデータのエクスポート
IgniteUI Excel Exporter を使用するには、IgxExcelExporterService
を app.module.ts ファイルにインポートし、providers
配列にサービスを追加します。
import { IgxExcelExporterService } from 'igniteui-angular';
@NgModule({
providers: [ IgxExcelExporterService ]
})
export class AppModule {}
typescript
v12.2.1 以降では、エクスポーター サービスは root で提供されます。つまり、AppModule プロバイダーでそれらを宣言する必要はありません。
エクスポート処理の開始は、コンポーネントのテンプレートでボタンのハンドラーを使用します。
<igx-grid #grid [data]="localData" [autoGenerate]="true"></igx-grid>
<button (click)="exportButtonHandler()">Export IgxGrid to Excel</button>
html
エクスポーター サービスへのアクセスは、コンポーネントのコンストラクターで IgxExcelExporterService
型の引数を定義し、Angular フレームワークはサービスのインスタンスを提供します。データを MS Excel 形式でエクスポートするには、エクスポーター サービスの export
メソッドを呼び出して IgxGrid コンポーネントを最初の引数として渡します。
以下のコードはコンポーネントの typescript ファイルでエクスポート処理を実行します。
import { IgxExcelExporterService, IgxExcelExporterOptions } from 'igniteui-angular';
import { IgxGridComponent } from 'igniteui-angular';
@ViewChild('grid') public grid: IgxGridComponent;
constructor(private excelExportService: IgxExcelExporterService) {
}
public exportButtonHandler() {
this.excelExportService.export(this.grid, new IgxExcelExporterOptions('ExportedDataFile'));
}
typescript
上記をすべて行うと、IgxGrid コンポーネントとその下にボタンを確認できます。ボタンを押すととエクスポート処理をトリガーし、ブラウザーで ExportedDataFile.xlsx ファイルをダウンロードします。このファイルは MS Excel 形式の Grid のデータを含みます。
すべてのデータのエクスポート
ページングなどのリモート操作を使用している場合に、Grid がすべてのデータにアクセスできない場合があります。このような場合、Excel Export サービスを使用してデータ コレクション全体を渡すことをお勧めします。例:
public exportButtonHandler() {
this.excelExportService.exportData(this.localData, new IgxExcelExporterOptions('ExportedDataFile'));
}
ts
グループ化されたデータのエクスポート
グループ化されたデータをエクスポートするには、Grid を 1 つ以上の列でグループ化する必要があります。ブラウザーは、選択した列でグループ化された MSExcel 形式の Grid コンポーネントからのデータを含む「ExportedDataFile.xlsx」という名前のファイルをダウンロードします。例:
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import {
IgxGridModule,
IgxExcelExporterService,
IgxButtonModule
} from "igniteui-angular";
import { ExcelExportSample1Component } from "./services/excel-export-sample-1/excel-export-sample-1.component";
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
ExcelExportSample1Component
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxGridModule,
IgxButtonModule
],
providers: [IgxExcelExporterService],
entryComponents: [],
schemas: []
})
export class AppModule {}
ts
import { Component, OnInit, ViewChild } from '@angular/core';
import {
GridColumnDataType,
IgxExcelExporterService,
IgxGridComponent,
ISortingExpression,
SortingDirection
} from 'igniteui-angular';
import { INVOICE_DATA } from '../data/invoiceData';
@Component({
selector: 'app-excel-export-sample-1',
styleUrls: ['./excel-export-sample-1.component.scss'],
templateUrl: './excel-export-sample-1.component.html'
})
export class ExcelExportSample1Component implements OnInit {
@ViewChild('igxGrid1', { static: true }) public igxGrid1: IgxGridComponent;
public data = [];
public groupExpressions: ISortingExpression[];
public columns: { dataType: GridColumnDataType, field: string, width: string, groupable: boolean, hidden?: boolean }[] = [
{ dataType: GridColumnDataType.String, field: 'ShipCountry', width: '150', groupable: true },
{ dataType: GridColumnDataType.String, field: 'ShipCity', width: '150', groupable: true },
{ dataType: GridColumnDataType.String, field: 'ShipAddress', width: '150', groupable: true},
{ dataType: GridColumnDataType.String, field: 'PostalCode', width: '150', groupable: true },
{ dataType: GridColumnDataType.Date, field: 'OrderDate', width: '150', groupable: true },
{ dataType: GridColumnDataType.Number, field: 'Quantity', width: '150', groupable: true }
];
constructor(private excelExportService: IgxExcelExporterService) {
}
public ngOnInit(): void {
this.data = INVOICE_DATA;
this.groupExpressions = [
{ dir: SortingDirection.Asc, fieldName: 'ShipCountry' },
{ dir: SortingDirection.Asc, fieldName: 'ShipCity' }
];
}
}
ts
<p class="grid__wrapper">
<igx-grid #igxGrid1 [data]="data" [groupingExpressions]="groupExpressions" [allowFiltering]="true" [cellSelection]="'single'"
[hideGroupedColumns]="true" [width]="'100%'" [height]="'700px'" [moving]="true">
<igx-grid-toolbar>
<igx-grid-toolbar-actions>
<igx-grid-toolbar-exporter [exportExcel]="true" [exportCSV]="false">
</igx-grid-toolbar-exporter>
</igx-grid-toolbar-actions>
</igx-grid-toolbar>
<igx-column *ngFor="let c of columns" [sortable]="true" [field]="c.field" [header]="c.field" [width]="c.width"
[hidden]="c.hidden" [groupable]="c.groupable" [editable]="true" [dataType]="c.dataType">
</igx-column>
</igx-grid>
</p>
html
.grid__wrapper {
padding-top: 16px;
width: 98% !important;
margin: 0 auto;
padding-left: 1%;
padding-right: 1%;
}
.exportButton {
margin-top: 5px;
}
scss

複数列ヘッダー グリッドのエクスポート
定義された複数列ヘッダーを使用して Grid をエクスポートできるようになりました。すべてのヘッダーは、Grid に表示されるときに、エクスポートされた Excel ファイルに反映されます。エクスポートされたデータから定義された複数列ヘッダーを除外する場合は、エクスポーター オプション ignoreMultiColumnHeaders を true
に設定できます。
Excel テーブルは複数の行ヘッダーをサポートしていないため、エクスポートされた Grid はテーブルとしてフォーマットされません。
import { NgModule } from "@angular/core";
import { FormsModule } from "@angular/forms";
import { BrowserModule } from "@angular/platform-browser";
import { BrowserAnimationsModule } from "@angular/platform-browser/animations";
import { AppComponent } from "./app.component";
import { GridMultiColumnHeadersExportComponent } from "./grid/multi-column-headers-export/multi-column-headers-export.component";
import {
IgxGridModule,
IgxExcelExporterService
} from "igniteui-angular";
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive";
@NgModule({
bootstrap: [AppComponent],
declarations: [
AppComponent,
GridMultiColumnHeadersExportComponent
],
imports: [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxGridModule
],
providers: [IgxExcelExporterService],
entryComponents: [],
schemas: []
})
export class AppModule {}
ts
import { Component, ViewChild } from '@angular/core';
import { IgxExporterEvent, IgxGridComponent } from 'igniteui-angular';
import { DATA } from '../../data/customers';
@Component({
selector: 'app-multi-column-headers-export',
styleUrls: [ 'multi-column-headers-export.component.scss' ],
templateUrl: 'multi-column-headers-export.component.html'
})
export class GridMultiColumnHeadersExportComponent {
@ViewChild(IgxGridComponent, { read: IgxGridComponent, static: true })
public grid: IgxGridComponent;
public data = DATA;
public exportHeaders = true;
public exportStarted(args: IgxExporterEvent) {
args.options.ignoreMultiColumnHeaders = !this.exportHeaders;
}
}
ts
<div class="grid__wrapper">
<igx-grid [igxPreventDocumentScroll]="true" #grid height="750px" [data]="data" [moving]="true" displayDensity="compact" [allowFiltering]="true">
<igx-grid-toolbar>
<igx-grid-toolbar-actions>
<igx-switch [(ngModel)]="exportHeaders">Export multi-column headers</igx-switch>
<igx-grid-toolbar-pinning></igx-grid-toolbar-pinning>
<igx-grid-toolbar-hiding></igx-grid-toolbar-hiding>
<igx-grid-toolbar-exporter [exportCSV]="false" [exportExcel]="true" (exportStarted)="exportStarted($event)">
</igx-grid-toolbar-exporter>
</igx-grid-toolbar-actions>
</igx-grid-toolbar>
<igx-column [resizable]="true" field="ID" [filterable]="false"></igx-column>
<igx-column-group header="General Information" [collapsible]="true" [expanded]="true">
<igx-column field="CompanyName" [visibleWhenCollapsed]="true"></igx-column>
<igx-column-group header="Personal Details" [collapsible]="true" [expanded]="false"
[visibleWhenCollapsed]="false">
<igx-column field="ContactName"></igx-column>
<igx-column field="ContactTitle"></igx-column>
</igx-column-group>
</igx-column-group>
<igx-column-group header="Address Information">
<igx-column-group header="Location" [collapsible]="true" [expanded]="false" [visibleWhenCollapsed]="true">
<igx-column field="Country" [visibleWhenCollapsed]="true" [hidden]="true"></igx-column>
<igx-column field="Region" [visibleWhenCollapsed]="false" [hidden]="true"></igx-column>
<igx-column field="City" [visibleWhenCollapsed]="false" [hidden]="true"></igx-column>
<igx-column field="Address" [visibleWhenCollapsed]="false" [hidden]="true"></igx-column>
</igx-column-group>
<igx-column-group header="Contact Information">
<igx-column field="Phone"></igx-column>
<igx-column field="Fax"></igx-column>
<igx-column field="PostalCode"></igx-column>
</igx-column-group>
</igx-column-group>
</igx-grid>
html
.grid__wrapper {
margin: 15px;
}
scss
固定された列ヘッダーを使用してグリッドをエクスポートする
デフォルトでは、Excel エクスポーター サービスは、スクロール可能な (固定されていない) 列ヘッダーを使用してグリッドをエクスポートします。エクスポートされた Excel ファイルの上にあるすべてのヘッダーを固定して、ユーザーがレコードをスクロールしても常に表示されたままにするシナリオがあります。これを実現するには、エクスポーター オプション freezeHeaders を true
に設定します。
public exportButtonHandler() {
const exporterOptions = new IgxExcelExporterOptions('ExportedDataFile');
exporterOptions.freezeHeaders = true;
this.excelExportService.export(this.grid, exporterOptions);
}
typescript
エクスポートするコンテンツのカスタマイズ
上記の例では、Excel Exporter サービスで利用可能なデータをすべてエクスポートしました。行または列全体のエクスポートをしない方が良い場合があります。実装は、各列で発生される columnExporting
または各行で発生される rowExporting
イベントを処理し、イベント引数オブジェクトの cancel
プロパティを true
に設定して各イベントをキャンセルします。
以下の例では、ヘッダーが "Age" で、インデックスが 1 の場合、エクスポートから列を除外します。
this.excelExportService.columnExporting.subscribe((args: IColumnExportingEventArgs) => {
if (args.header == 'Age' && args.columnIndex == 1) {
args.cancel = true;
}
});
this.excelExportService.export(this.grid, new IgxExcelExporterOptions('ExportedDataFile'));
typescript
Grid コンポーネントのデータ エクスポートでは、行フィルタリングおよび列の非表示などの機能に応じて Grid で表示されるデータのみをエクスポートします。IgxExcelExporterOptions
オブジェクトのプロパティを設定し、エクスポーター サービスを構成してフィルターした行または非表示の列を含むことができます。
既知の制限
制限 |
説明 |
ワークシートの最大サイズ |
Excel でサポートされているワークシートの最大サイズは、1,048,576 行 x 16,384 列です。 |
セルのスタイル設定 |
IgxExcelExporterService は、セル コンポーネントに適用されたカスタム スタイルのエクスポートをサポートしていません。このような場合は、Excel Library を使用することをお勧めします。 |
API リファレンス
以下は、その他の Excel Exporter サービスの API です。
使用したその他のコンポーネント:
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。