如何在.NET Core中使用Aspose.Cells.GridWeb
Contents
[
Hide
]
本主题解释了如何在使用Visual Studio.NET 2019的.NET Core应用程序中使用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 Packages 添加到项目中
- 添加 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
- 在 Controllers 中添加 AcwController,以处理提供一般编辑操作的 acw 路由映射
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: 测试该应用程序
运行该应用程序将产生类似于下图所示的输出