Skip to content

Commit

Permalink
Excel ライブラリの .dll に対し、遅延読み込みを適用
Browse files Browse the repository at this point in the history
  • Loading branch information
jsakamotoIGJP committed Jun 24, 2022
1 parent 6bd93cb commit bb96512
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
64 changes: 43 additions & 21 deletions Client/App.razor
@@ -1,7 +1,9 @@
@using System.Text.Json
@using Microsoft.AspNetCore.Components.WebAssembly.Services
@using Toolbelt.Blazor.Extensions
@inject HttpClient HttpClient
@inject IJSRuntime JSRuntime
@inject LazyAssemblyLoader AssemblyLoader

<h1>直近1週間の1日ごと地震発生回数</h1>

Expand Down Expand Up @@ -60,31 +62,51 @@
this._Processing = true;
try
{
if (this._EarthquakeCountParDays == null) return;
// プロジェクトファイル (IgbExcelDemo.Client.csproj) にて遅延読み込みを指定しておいた
// アセンブリファイル (.dll) を、ここで AssemblyLoader を使って読み込みます。
// (補足: 繰り返し何度も呼び出しても大丈夫です、まだいちども読み込まれていない .dll のみが読み込まれます)
await this.AssemblyLoader.LoadAssembliesAsync(new[]
{
"IgniteUI.Blazor.Documents.Core.dll",
"IgniteUI.Blazor.Documents.Excel.dll"
});

// アセンブリの遅延読み込みを行なってから、その遅延読み込みされるアセンブリ内の機能を使うメソッドを呼び出します。
// (アセンブリの遅延読み込みを行なうメソッド内で直接、遅延読み込みしたアセンブリ内の機能を参照していると、
// System.IO.FileNotFoundException: Could not load file or assembly 例外が発生します。)
await this.DownloadAsExcelAsync();
}
finally { this._Processing = false; }
}

/// <summary>
/// アセンブリの遅延読み込みを有効にするために、Excel ライブラリを使用するコードを独立したメソッドに切り出します。
/// </summary>
private async ValueTask DownloadAsExcelAsync()
{
if (this._EarthquakeCountParDays == null) return;

// ⚠️注意 - Blazor WebAssembly 上で Excel ライブラリを使うには、Workbook.InProcessRuntime 静的プロパティの初期設定が必要です。
if (Workbook.InProcessRuntime == null) Workbook.InProcessRuntime = this.JSRuntime as IJSInProcessRuntime;
// ⚠️注意 - Blazor WebAssembly 上で Excel ライブラリを使うには、Workbook.InProcessRuntime 静的プロパティの初期設定が必要です。
if (Workbook.InProcessRuntime == null) Workbook.InProcessRuntime = this.JSRuntime as IJSInProcessRuntime;

// 雛形の Excel ファイルをサーバーから取得し、Workbook オブジェクトに読み込みます。
await using var templateStream = await this.HttpClient.GetStreamAsync("./TemplateBook.xlsx");
var workBook = Workbook.Load(templateStream);
await templateStream.DisposeAsync();
// 雛形の Excel ファイルをサーバーから取得し、Workbook オブジェクトに読み込みます。
await using var templateStream = await this.HttpClient.GetStreamAsync("./TemplateBook.xlsx");
var workBook = Workbook.Load(templateStream);
await templateStream.DisposeAsync();

// シートの中身 (セル) に、地震発生の日付と回数を転記していきます。
var sheet = workBook.Worksheets[0];
var row = 2;
foreach (var item in this._EarthquakeCountParDays)
{
sheet.GetCell($"A{row}").Value = item.Date;
sheet.GetCell($"B{row}").Value = item.Count;
row++;
}
// シートの中身 (セル) に、地震発生の日付と回数を転記していきます。
var sheet = workBook.Worksheets[0];
var row = 2;
foreach (var item in this._EarthquakeCountParDays)
{
sheet.GetCell($"A{row}").Value = item.Date;
sheet.GetCell($"B{row}").Value = item.Count;
row++;
}

// 記入が終わった Workbook オブジェクトを .xlsx ファイル形式に書き出し、ブラウザにダウンロードさせます。
await using var memStream = new MemoryStream();
workBook.Save(memStream);
// 記入が終わった Workbook オブジェクトを .xlsx ファイル形式に書き出し、ブラウザにダウンロードさせます。
await using var memStream = new MemoryStream();
workBook.Save(memStream);
await this.JSRuntime.InvokeDownloadAsync("Book.xlsx", "application/octet-stream", memStream.ToArray());
}
finally { this._Processing = false; }
}
}
6 changes: 6 additions & 0 deletions Client/IgbExcelDemo.Client.csproj
Expand Up @@ -14,4 +14,10 @@
<PackageReference Include="Toolbelt.Blazor.InvokeDownloadAsync" Version="1.0.0" />
</ItemGroup>

<!-- 遅延読み込みさせたいアセンブリファイル (.dll) のファイル名を、 BlazorWebAssemblyLazyLoad 要素で指定します。 -->
<ItemGroup>
<BlazorWebAssemblyLazyLoad Include="IgniteUI.Blazor.Documents.Core.dll" />
<BlazorWebAssemblyLazyLoad Include="IgniteUI.Blazor.Documents.Excel.dll" />
</ItemGroup>

</Project>

0 comments on commit bb96512

Please sign in to comment.