.NET Core でAspose.Cells.GridWebを使用する方法
Contents
[
Hide
]
このトピックでは、Visual Studio.NET 2019を使用して.NET CoreアプリケーションでAspose.Cells.GridWebを使用する方法について説明します。このトピックは、Aspose.Cells.GridWebで作業する初心者レベルの開発者に役立ちます。
.NET CoreでAspose.Cells.GridWebを使用する
このトピックでは、Visual Studio 2019でサンプルのウェブサイトを作成することで、Aspose.Cells.GridWebの使用方法を示します。プロセスはステップに分かれています。
ステップ1:新しいプロジェクトの作成
- Visual Studio 2019を開きます。
- ファイルメニューから新規、次にプロジェクトを選択します。 新しいプロジェクトダイアログが開きます。
- Visual StudioにインストールされたプロジェクトテンプレートからASP.NET Core Webアプリケーションを選択し、次へをクリックします。
- プロジェクトの場所と名前を指定し、作成をクリックします。
- **Webアプリケーション(モデル-ビュー-コントローラ)**テンプレートを選択し、ASP .NET Core 2.1が選択されていることを確認します。
- 作成をクリックします。
ステップ2:初期ビューの確認
新しく作成したプロジェクトを実行すると、ブラウザにデフォルトのテンプレートが表示されます。
ステップ3:Aspose.Cells.GridWebの追加
- 以下のNugetパッケージをプロジェクトに追加します
- Aspose.Cells.GridWebパッケージを追加します
- Viewsフォルダの**_ViewImports.cshtml**ファイルに以下を追加します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
@using Aspose.Cells.GridWeb @addTagHelper *, Aspose.Cells.GridWeb
変更後のファイルは以下のようになります
- HomeControllerのIndexメソッドに以下のコードを入れます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//set a session store path | |
GridWeb.SessionStorePath = @"D:\Test\tmp\"; | |
GridWeb mw = new GridWeb(); | |
mw.ID = "gid"; | |
mw.SetSession(HttpContext.Session); | |
//set acw_client path | |
mw.ResourceFilePath = "/js/acw_client/"; | |
//load workbook | |
mw.ImportExcelFile("D:\\Book1.xlsx"); | |
//set width height | |
mw.Width = Unit.Pixel(800); | |
mw.Height = Unit.Pixel(500); | |
return View(mw); |
SessionStorePathとImportExcelFileのパスを更新することを忘れないでください。
- View > HomeディレクトリのIndex.cshtmlファイルに以下のコードを追加します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@model GridWeb | |
<script src="~/js/acw_client/acwmain.js" asp-append-version="true"></script> | |
<script src="~/js/acw_client/lang_en.js" asp-append-version="true"></script> | |
<link href="~/js/acw_client/menu.css" rel="stylesheet" type="text/css"> | |
<div class="text-center"> | |
<GridWebDiv mw=Model></GridWebDiv> | |
</div> |
変更後のファイルは以下のようになります
- Startup.csファイルにSessionサポートとGridScheduedServiceを追加します
- ConfigureServicesメソッドに以下のコードスニペットを追加します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
services.AddSession(options => | |
{ | |
// Set a short timeout for easy testing. | |
options.IdleTimeout = TimeSpan.FromSeconds(3600); | |
options.Cookie.HttpOnly = true; | |
// Make the session cookie essential | |
options.Cookie.IsEssential = true; | |
}); | |
services.AddSingleton<Microsoft.Extensions.Hosting.IHostedService, GridScheduedService>(); |
- Configureメソッドに以下のコードスニペットを追加します。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
app.UseSession(); | |
app.UseMvc(routes => | |
{ | |
routes.MapRoute("acw", "acw/{type}/{id}", | |
defaults: new { controller = "Acw", action = "Operation" }); | |
routes.MapRoute( | |
name: "default", | |
template: "{controller=Home}/{action=Index}/{id?}"); | |
}); |
最新のacw_clientをwwwroot/jsディレクトリに置きます
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AcwController : Controller | |
{ | |
public IActionResult Operation(string type, string id) | |
{ | |
return Aspose.Cells.GridWeb.AcwController.DoAcwAction(this, type, id); | |
} | |
} |
ステップ4:アプリのテスト
アプリを実行すると、以下の画像に示すような出力が表示されます。