Skip to content

Commit 8927473

Browse files
add a Spreadsheet example where sheet is loaded from an .xlsx file on… (#76)
* add a Spreadsheet example where sheet is loaded from an .xlsx file on the server
1 parent 9d2763a commit 8927473

File tree

7 files changed

+74
-1
lines changed

7 files changed

+74
-1
lines changed

Telerik.Examples.Mvc/NuGet.config

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
<?xml version="1.0" encoding="utf-8"?>
1+
<?xml version="1.0" encoding="utf-8"?>
22
<configuration>
33
<packageSources>
44
<clear /> <!-- ensure only the sources defined below are used -->
55
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
66
<add key="Telerik.UI.for.AspNet.Core.Trial.2022.2.510" value=".\\nuget" />
7+
<add key="Telerik" value="https://nuget.telerik.com/v3/index.json" />
8+
<add key="Telerik.Web.Spreadsheet.Trial.2023.1.426" value=".\\nuget" />
79
</packageSources>
810
</configuration>
911

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.AspNetCore.Hosting;
2+
using Microsoft.AspNetCore.Mvc;
3+
using NuGet.ContentModel;
4+
using System.IO;
5+
using Telerik.SvgIcons;
6+
using Telerik.Web.Spreadsheet;
7+
8+
namespace Telerik.Examples.Mvc.Controllers.Spreadsheet
9+
{
10+
public class Spreadsheet_Load_Xlsx_From_ServerController : Controller
11+
{
12+
public IActionResult Spreadsheet_Load_Xlsx_From_Server()
13+
{
14+
return View("~/Views/Spreadsheet/Spreadsheet_Load_Xlsx_From_Server.cshtml");
15+
}
16+
private readonly IWebHostEnvironment _hostingEnvironment;
17+
18+
public Spreadsheet_Load_Xlsx_From_ServerController(IWebHostEnvironment hostingEnvironment)
19+
{
20+
_hostingEnvironment = hostingEnvironment;
21+
}
22+
23+
public IActionResult ReadFile()
24+
{
25+
string filePath = Path.Combine(_hostingEnvironment.WebRootPath, "files", "Test.xlsx");
26+
var exists = System.IO.File.Exists(filePath);
27+
if (System.IO.File.Exists(filePath))
28+
{
29+
Stream fileStream = System.IO.File.OpenRead(filePath);
30+
var workbook = Workbook.Load(fileStream, Path.GetExtension(filePath));
31+
return Content(workbook.ToJson(), Telerik.Web.Spreadsheet.MimeTypes.JSON);
32+
}
33+
else
34+
{
35+
return Content("Request file does not exist.");
36+
}
37+
}
38+
}
39+
}

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Program.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
options.ViewLocationFormats.Add("/Views/MultiSelect/{0}" + RazorViewEngine.ViewExtension);
5858
options.ViewLocationFormats.Add("/Views/Scheduler/{0}" + RazorViewEngine.ViewExtension);
5959
options.ViewLocationFormats.Add("/Views/StylesAndLayout/{0}" + RazorViewEngine.ViewExtension);
60+
options.ViewLocationFormats.Add("/Views/Spreadsheet/{0}" + RazorViewEngine.ViewExtension);
6061
});
6162

6263
builder.Services.AddDbContext<GeneralDbContext>(options =>

Telerik.Examples.Mvc/Telerik.Examples.Mvc/Telerik.Examples.Mvc.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
<PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="7.0.0" />
2222
<PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="7.0.6" />
2323
<PackageReference Include="System.Drawing.Common" Version="7.0.0" />
24+
<PackageReference Include="Telerik.Web.Spreadsheet.Trial" Version="2023.1.426" />
2425
<PackageReference Include="Telerik.UI.for.AspNet.Core.Trial" Version="2023.3.1114" />
2526
<PackageReference Include="Telerik.Web.Captcha.Trial" Version="1.1.2" />
2627
</ItemGroup>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
@(Html.Kendo().Spreadsheet()
2+
.Name("spreadsheet")
3+
.HtmlAttributes(new { style = "width:100%" })
4+
.Toolbar(false)
5+
.Sheetsbar(false)
6+
.Excel(excel => excel
7+
.ProxyURL(Url.Action("Index_Save", "Spreadsheet"))
8+
)
9+
.Pdf(pdf => pdf
10+
.ProxyURL(Url.Action("Index_Save", "Spreadsheet"))
11+
)
12+
)
13+
14+
<script>
15+
$(document).ready(function(){
16+
readFile();
17+
})
18+
function readFile(){
19+
$.ajax({
20+
url: "@Url.Action("ReadFile","Spreadsheet_Load_Xlsx_From_Server")",
21+
success: function (e) {
22+
var spreadsheet = $("#spreadsheet").data("kendoSpreadsheet");
23+
spreadsheet.fromJSON(e);
24+
},
25+
error: function (er) {
26+
console.log(er);
27+
}
28+
})
29+
}
30+
</script>
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)