如何将 Aspose.Cells.GridWeb 与 .NET 核心一起使用
Contents
[
Hide
]
本主题说明如何将 Aspose.Cells.GridWeb 与 .NET Core 应用程序一起使用 Visual Studio.NET 2019。本主题对使用 Aspose.Cells.GridWeb 的初级开发人员很有用。
将 Aspose.Cells.GridWeb 与 .NET 核心一起使用
本主题通过在 Visual Studio 2019 中制作示例网站来展示如何使用 Aspose.Cells.GridWeb。过程分为几个步骤。
第 1 步:创建一个新项目
- 打开 Visual Studio 2019。
- 来自文件菜单,选择新的, 然后项目. 创建一个新项目对话框打开。
- 选择ASP.NET 核心网络应用程序从 Visual Studio 安装的项目模板并单击下一个.
- 指定项目的位置和名称,然后单击创造.
- 选择Web 应用程序(模型-视图-控制器)模板并确保ASP .NET 核心 2.1被选中。
- 点击创造.
第 2 步:检查初始视图
运行新创建的项目会在浏览器中显示默认模板,如下图所示。
第 3 步:添加 Aspose.Cells.GridWeb
- 将以下 Nuget 包添加到项目中
- 添加 Aspose.Cells.GridWeb 包
- 将以下内容添加到 Views 文件夹中的 _ViewImports.cshtml 文件。
This file contains hidden or 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 hidden or 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 路径。
- 在中添加以下代码索引.cshtml View > Home 目录中的文件。
This file contains hidden or 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 hidden or 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 hidden or 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 中处理 acw route map 可以提供一般编辑动作的所有默认操作。
This file contains hidden or 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 步:测试应用程序
运行应用程序将输出类似于下图所示的输出。