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
- Open Visual Studio 2019.
- From the File menu, select New, then Project. Create a new project dialog is opened.
- Select ASP.NET Core Web Application from Visual Studio installed project templates and click Next.
- Specify a location where the location and the name of the project and click Create.
- Select the Web Application (Model-View-Controller) template and make sure that ASP .NET Core 2.1 is selected.
- 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.
Step 3: Adding Aspose.Cells.GridWeb
- Add the following Nuget Packages to the project
- Add Aspose.Cells.GridWeb Package
- Add the following to the _ViewImports.cshtml file in the Views folder.
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
The file will look like this after the modifications
- 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); |
- 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.
- Add Session support and GridScheduedService in the Startup.cs file
- 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>(); |
- 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?}"); | |
}); |
- Put the latest acw_client in directory: wwwroot/js
- 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); | |
} | |
} |
Step 4: Test the App
Running the app will the output similar to the one shown in the image below.