.NET Core でAspose.Cells.GridWebを使用する方法

.NET CoreでAspose.Cells.GridWebを使用する

このトピックでは、Visual Studio 2019でサンプルのウェブサイトを作成することで、Aspose.Cells.GridWebの使用方法を示します。プロセスはステップに分かれています。

ステップ1:新しいプロジェクトの作成

  1. Visual Studio 2019を開きます。
  2. ファイルメニューから新規、次にプロジェクトを選択します。 新しいプロジェクトダイアログが開きます。
  3. Visual StudioにインストールされたプロジェクトテンプレートからASP.NET Core Webアプリケーションを選択し、次へをクリックします。

todo:image_alt_text

  1. プロジェクトの場所と名前を指定し、作成をクリックします。

todo:image_alt_text

  1. **Webアプリケーション(モデル-ビュー-コントローラ)**テンプレートを選択し、ASP .NET Core 2.1が選択されていることを確認します。

todo:image_alt_text

  1. 作成をクリックします。

ステップ2:初期ビューの確認

新しく作成したプロジェクトを実行すると、ブラウザにデフォルトのテンプレートが表示されます。

todo:image_alt_text

ステップ3:Aspose.Cells.GridWebの追加

  1. 以下のNugetパッケージをプロジェクトに追加します

  1. Aspose.Cells.GridWebパッケージを追加します

todo:image_alt_text

  1. Viewsフォルダの**_ViewImports.cshtml**ファイルに以下を追加します。
    @using Aspose.Cells.GridWeb
    @addTagHelper *, Aspose.Cells.GridWeb

変更後のファイルは以下のようになります

todo:image_alt_text

  1. HomeControllerのIndexメソッドに以下のコードを入れます。
//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);

todo:image_alt_text

  1. View > HomeディレクトリのIndex.cshtmlファイルに以下のコードを追加します。
@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>

変更後のファイルは以下のようになります

todo:image_alt_text

  1. Startup.csファイルにSessionサポートとGridScheduedServiceを追加します
    1. ConfigureServicesメソッドに以下のコードスニペットを追加します。
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>();

todo:image_alt_text

  1. Configureメソッドに以下のコードスニペットを追加します。
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?}");
});

todo:image_alt_text

最新のacw_clientをwwwroot/jsディレクトリに置きます

一般的な編集操作のすべてのデフォルト操作を提供できるacwルートマップを処理するAcwControllerを追加します。

public class AcwController : Controller
{
public IActionResult Operation(string type, string id)
{
return Aspose.Cells.GridWeb.AcwController.DoAcwAction(this, type, id);
}
}

todo:image_alt_text

ステップ4:アプリのテスト

アプリを実行すると、以下の画像に示すような出力が表示されます。

todo:image_alt_text