Angular Tree Grid Excel を Excel サービスへエクスポート
Excel Exporter サービスは IgxTreeGrid のデータを MS Excel へエクスポートできます。エクスポート機能は、IgxExcelExporterService
クラスでカプセル化され、MS Excel テーブル形式でデータをエクスポートします。この形式ではフィルタリングやソートなどの機能が使用でき、IgxExcelExporterService
の export
メソッドを呼び出して最初の引数として IgxTreeGrid コンポーネントを渡し、グリッドを簡単にエクスポートします。
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 {
IgxTreeGridModule,
IgxExcelExporterService,
IgxButtonModule
} from "igniteui-angular" ;
import { TreeGridExcelExportSample1Component } from "./services/export-excel/excel-export-tree-grid-sample/excel-export-tree-grid-sample.component" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
TreeGridExcelExportSample1Component
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxTreeGridModule,
IgxButtonModule
],
providers : [IgxExcelExporterService],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component, OnInit, ViewChild } from '@angular/core' ;
import { IgxTreeGridComponent } from 'igniteui-angular' ;
import { FOODS_DATA } from '../../../tree-grid/data/foods' ;
@Component ({
selector : 'app-excel-export-tree-grid-sample' ,
styleUrls : ['./excel-export-tree-grid-sample.component.scss' ],
templateUrl : './excel-export-tree-grid-sample.component.html'
})
export class TreeGridExcelExportSample1Component implements OnInit {
@ViewChild ('igxTreeGrid1' , { static : true })
public igxTreeGrid1: IgxTreeGridComponent;
public data: any [];
constructor ( ) { }
public ngOnInit(): void {
this .data = FOODS_DATA();
}
}
ts コピー <p class ="grid__wrapper" >
<igx-tree-grid [igxPreventDocumentScroll ]="true" #igxTreeGrid1 [data ]="data" [autoGenerate ]="false" height ="280px" width ="100%"
primaryKey ="ID" foreignKey ="ParentID" >
<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 field ="ID" header ="Product ID" [dataType ]="'string'" >
</igx-column >
<igx-column field ="Name" header ="Product Name" [dataType ]="'string'" >
</igx-column >
<igx-column field ="UnitPrice" header ="Unit Price" dataType ="number" >
<ng-template igxCell let-cell ="cell" let-val >
<span *ngIf ="cell.row.data.UnitPrice === 0" > -</span >
<span *ngIf ="cell.row.data.UnitPrice !== 0" > ${{val}}</span >
</ng-template >
</igx-column >
<igx-column field ="AddedDate" header ="Added Date" [dataType ]="'date'" >
</igx-column >
<igx-column field ="Discontinued" header ="Discontinued" [dataType ]="'boolean'" >
<ng-template igxCell let-cell ="cell" let-val >
<span *ngIf ="cell.row.data.UnitPrice === 0" > -</span >
<img *ngIf ="cell.row.data.UnitPrice !== 0 && val" src ="https://www.infragistics.com/angular-demos-lob/assets/images/grid/active.png" title ="Continued" alt ="Continued" />
<img *ngIf ="cell.row.data.UnitPrice !== 0 && !val" src ="https://www.infragistics.com/angular-demos-lob/assets/images/grid/expired.png" title ="Discontinued" alt ="Discontinued" />
</ng-template >
</igx-column >
</igx-tree-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ツールキットにアクセスして、すばやく独自のアプリの作成を開始します。無料でダウンロードできます。
Tree 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-tree-grid #treeGrid [data ]="localData" [autoGenerate ]="true" > </igx-tree-grid >
<button (click )="exportButtonHandler()" > Export IgxTreeGrid to Excel</button >
html
エクスポーター サービスへのアクセスは、コンポーネントのコンストラクターで IgxExcelExporterService
型の引数を定義し、Angular フレームワークはサービスのインスタンスを提供します。データを MS Excel 形式でエクスポートするには、エクスポーター サービスの export
メソッドを呼び出して IgxTreeGrid コンポーネントを最初の引数として渡します。
以下のコードはコンポーネントの typescript ファイルでエクスポート処理を実行します。
import { IgxExcelExporterService, IgxExcelExporterOptions } from 'igniteui-angular' ;
import { IgxTreeGridComponent } from 'igniteui-angular' ;
@ViewChild ('treeGrid' ) public treeGrid: IgxTreeGridComponent;
constructor (private excelExportService: IgxExcelExporterService ) {
}
public exportButtonHandler ( ) {
this .excelExportService.export(this .treeGrid, new IgxExcelExporterOptions('ExportedDataFile' ));
}
typescript
上記をすべて行うと、IgxTreeGrid コンポーネントとその下にボタンを確認できます。ボタンを押すととエクスポート処理をトリガーし、ブラウザーで ExportedDataFile.xlsx ファイルをダウンロードします。このファイルは MS Excel 形式の Tree Grid のデータを含みます。
すべてのデータのエクスポート
ページングなどのリモート操作を使用している場合に、Grid がすべてのデータにアクセスできない場合があります。このような場合、Excel Export サービス を使用してデータ コレクション全体を渡すことをお勧めします。例:
public exportButtonHandler ( ) {
this .excelExportService.exportData(this .localData, new IgxExcelExporterOptions('ExportedDataFile' ));
}
ts
複数列ヘッダー グリッドのエクスポート
定義された複数列ヘッダー を使用して Tree Grid をエクスポートできるようになりました。すべてのヘッダーは、Tree Grid に表示されるときに、エクスポートされた Excel ファイルに反映されます。エクスポートされたデータから定義された複数列ヘッダーを除外する場合は、エクスポーター オプション ignoreMultiColumnHeaders を true
に設定できます。
Excel テーブルは複数の行ヘッダーをサポートしていないため、エクスポートされた Tree 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 { TreeGridMultiColumnHeadersExportComponent } from "./tree-grid/tree-grid-multi-column-headers-export/tree-grid-multi-column-headers-export.component" ;
import {
IgxTreeGridModule,
IgxExcelExporterService
} from "igniteui-angular" ;
import { IgxPreventDocumentScrollModule } from "./directives/prevent-scroll.directive" ;
@NgModule ({
bootstrap : [AppComponent],
declarations : [
AppComponent,
TreeGridMultiColumnHeadersExportComponent
],
imports : [
BrowserModule,
BrowserAnimationsModule,
FormsModule,
IgxPreventDocumentScrollModule,
IgxTreeGridModule
],
providers : [IgxExcelExporterService],
entryComponents : [],
schemas : []
})
export class AppModule {}
ts コピー import { Component } from '@angular/core' ;
import { GridSelectionMode, IgxExporterEvent } from 'igniteui-angular' ;
import { generateEmployeeDetailedFlatData } from '../data/employees-flat-detailed' ;
@Component ({
selector : 'app-tree-grid-multi-column-headers-export-sample' ,
styleUrls : ['./tree-grid-multi-column-headers-export.component.scss' ],
templateUrl : './tree-grid-multi-column-headers-export.component.html'
})
export class TreeGridMultiColumnHeadersExportComponent {
public data = generateEmployeeDetailedFlatData();
public selectionMode: GridSelectionMode = 'none' ;
public exportHeaders = true ;
public exportStarted (args: IgxExporterEvent ) {
args.options.ignoreMultiColumnHeaders = !this .exportHeaders;
}
}
ts コピー <div class ="grid__wrapper" >
<igx-tree-grid [igxPreventDocumentScroll ]="true" #treeGrid [data ]="data" primaryKey ="ID" [moving ]="true" foreignKey ="ParentID" [rowSelection ]="selectionMode" height ="740px"
displayDensity ="compact" [allowFiltering ]="true" width ="100%" >
<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 field ="ID" dataType ="number" [resizable ]="true" [filterable ]="false" > </igx-column >
<igx-column field ="Name" dataType ="string" [sortable ]="true" [resizable ]="true" width ="200px" > </igx-column >
<igx-column-group [pinned ]="false" header ="General Information" >
<igx-column field ="HireDate" dataType ="date" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column-group header ="Personal Details" >
<igx-column field ="Title" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column field ="Age" dataType ="number" [sortable ]="true" [resizable ]="true" > </igx-column >
</igx-column-group >
</igx-column-group >
<igx-column-group header ="Address Information" >
<igx-column-group header ="Location" >
<igx-column field ="Country" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column field ="City" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column field ="Address" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
</igx-column-group >
<igx-column-group header ="Contact Information" >
<igx-column field ="Phone" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column field ="Fax" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
<igx-column field ="PostalCode" dataType ="string" [sortable ]="true" [resizable ]="true" > </igx-column >
</igx-column-group >
</igx-column-group >
</igx-tree-grid >
</div >
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 .treeGrid, new IgxExcelExporterOptions('ExportedDataFile' ));
typescript
Tree Grid コンポーネントのデータ エクスポートでは、行フィルタリングおよび列の非表示などの機能に応じて Tree Grid で表示されるデータのみをエクスポートします。IgxExcelExporterOptions
オブジェクトのプロパティを設定し、エクスポーター サービスを構成してフィルターした行または非表示の列を含むことができます。
既知の制限
制限
説明
階層レベル
Excel エクスポーター サービスは、最大 8 レベルの階層を作成できます。
ワークシートの最大サイズ
Excel でサポートされているワークシートの最大サイズは、1,048,576 行 x 16,384 列です。
セルのスタイル設定
IgxExcelExporterService は、セル コンポーネントに適用されたカスタム スタイルのエクスポートをサポートしていません。このような場合は、Excel Library を使用することをお勧めします。
API リファレンス
以下は、その他の Excel Exporter サービスの API です。
使用したその他のコンポーネント:
その他のリソース
コミュニティに参加して新しいアイデアをご提案ください。