How to use Aspose.Cells.GridWeb with .NET Core

Use Aspose.Cells.GridWeb with .NET Core

This topic shows how to use Aspose.Cells.GridWeb by making a sample website in Visual Studio 2019. The process has been divided into steps.

Step 1: Creating a New Project

  1. Open Visual Studio 2019.
  2. From the File menu, select New, then Project. Create a new project dialog is opened.
  3. Select ASP.NET Core Web Application from Visual Studio installed project templates and click Next.

todo:image_alt_text

  1. Specify a location where the location and the name of the project and click Create.

todo:image_alt_text

  1. Select the Web Application (Model-View-Controller) template and make sure that ASP .NET Core 2.1 is selected.

todo:image_alt_text

  1. Click Create.

Step 2: Checking the initial view

Running the newly created project shows the default template in the browser as shown in the image below.

todo:image_alt_text

Step 3: Adding Aspose.Cells.GridWeb

  1. Add the following Nuget Packages to the project

  1. Add Aspose.Cells.GridWeb Package

todo:image_alt_text

  1. Add the following to the _ViewImports.cshtml file in the Views folder.
    @using Aspose.Cells.GridWeb
    @addTagHelper *, Aspose.Cells.GridWeb

The file will look like this after the modifications

todo:image_alt_text

  1. Put the following code in the HomeController’s Index method.
//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. Add the following code in the Index.cshtml file in the View > Home directory.
@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>

The file will look like this after the change.

todo:image_alt_text

  1. Add Session support and GridScheduedService in the Startup.cs file
    1. Add the following code snippet in the ConfigureServices method.
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. Add the following code snippet in the Configure method.
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

  1. Put the latest acw_client in directory: wwwroot/js
  2. Add AcwController in Controllers to deal with the acw route map that can provide all the default operations for general edit action.
public class AcwController : Controller
{
public IActionResult Operation(string type, string id)
{
return Aspose.Cells.GridWeb.AcwController.DoAcwAction(this, type, id);
}
}

todo:image_alt_text

Step 4: Test the App

Running the app will the output similar to the one shown in the image below.

todo:image_alt_text