This repository has been archived by the owner on Jan 25, 2019. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
0 deletions.
There are no files selected for viewing
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
105 changes: 105 additions & 0 deletions
105
src/EPPlus.Core.SampleWebApp/Controllers/HomeController.cs
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
using System.IO; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.AspNetCore.Mvc; | ||
using OfficeOpenXml; | ||
using OfficeOpenXml.Table; | ||
|
||
namespace EPPlus.Core.SampleWebApp.Controllers | ||
{ | ||
public class HomeController : Controller | ||
{ | ||
private const string XlsxContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; | ||
private readonly IHostingEnvironment _hostingEnvironment; | ||
|
||
public HomeController(IHostingEnvironment hostingEnvironment) | ||
{ | ||
_hostingEnvironment = hostingEnvironment; | ||
} | ||
|
||
/// <summary> | ||
/// /Home/FileReport | ||
/// </summary> | ||
public IActionResult FileReport() | ||
{ | ||
var fileDownloadName = "report.xlsx"; | ||
var reportsFolder = "reports"; | ||
|
||
using (var package = getExcelPackage()) | ||
{ | ||
package.SaveAs(new FileInfo(Path.Combine(_hostingEnvironment.WebRootPath, reportsFolder, fileDownloadName))); | ||
} | ||
return File($"~/{reportsFolder}/{fileDownloadName}", XlsxContentType, fileDownloadName); | ||
} | ||
|
||
/// <summary> | ||
/// An in-memory report | ||
/// </summary> | ||
public IActionResult Index() | ||
{ | ||
byte[] reportBytes; | ||
using (var package = getExcelPackage()) | ||
{ | ||
reportBytes = package.GetAsByteArray(); | ||
} | ||
|
||
return File(reportBytes, XlsxContentType, "report.xlsx"); | ||
} | ||
|
||
private ExcelPackage getExcelPackage() | ||
{ | ||
var package = new ExcelPackage(); | ||
package.Workbook.Properties.Title = "Salary Report"; | ||
package.Workbook.Properties.Author = "Vahid N."; | ||
package.Workbook.Properties.Subject = "Salary Report"; | ||
package.Workbook.Properties.Keywords = "Salary"; | ||
|
||
|
||
var worksheet = package.Workbook.Worksheets.Add("Employee"); | ||
|
||
//First add the headers | ||
worksheet.Cells[1, 1].Value = "ID"; | ||
worksheet.Cells[1, 2].Value = "Name"; | ||
worksheet.Cells[1, 3].Value = "Gender"; | ||
worksheet.Cells[1, 4].Value = "Salary (in $)"; | ||
|
||
//Add values | ||
|
||
var numberformat = "#,##0"; | ||
var dataCellStyleName = "TableNumber"; | ||
var numStyle = package.Workbook.Styles.CreateNamedStyle(dataCellStyleName); | ||
numStyle.Style.Numberformat.Format = numberformat; | ||
|
||
worksheet.Cells[2, 1].Value = 1000; | ||
worksheet.Cells[2, 2].Value = "Jon"; | ||
worksheet.Cells[2, 3].Value = "M"; | ||
worksheet.Cells[2, 4].Value = 5000; | ||
worksheet.Cells[2, 4].Style.Numberformat.Format = numberformat; | ||
|
||
worksheet.Cells[3, 1].Value = 1001; | ||
worksheet.Cells[3, 2].Value = "Graham"; | ||
worksheet.Cells[3, 3].Value = "M"; | ||
worksheet.Cells[3, 4].Value = 10000; | ||
worksheet.Cells[3, 4].Style.Numberformat.Format = numberformat; | ||
|
||
worksheet.Cells[4, 1].Value = 1002; | ||
worksheet.Cells[4, 2].Value = "Jenny"; | ||
worksheet.Cells[4, 3].Value = "F"; | ||
worksheet.Cells[4, 4].Value = 5000; | ||
worksheet.Cells[4, 4].Style.Numberformat.Format = numberformat; | ||
|
||
// Add to table / Add summary row | ||
var tbl = worksheet.Tables.Add(new ExcelAddressBase(fromRow: 1, fromCol: 1, toRow: 4, toColumn: 4), "Data"); | ||
tbl.ShowHeader = true; | ||
tbl.TableStyle = TableStyles.Dark9; | ||
tbl.ShowTotal = true; | ||
tbl.Columns[3].DataCellStyleName = dataCellStyleName; | ||
tbl.Columns[3].TotalsRowFunction = RowFunctions.Sum; | ||
worksheet.Cells[5, 4].Style.Numberformat.Format = numberformat; | ||
|
||
// AutoFitColumns | ||
worksheet.Cells[1, 1, 4, 4].AutoFitColumns(); | ||
|
||
return package; | ||
} | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
src/EPPlus.Core.SampleWebApp/EPPlus.Core.SampleWebApp.csproj
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<Project Sdk="Microsoft.NET.Sdk.Web"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netcoreapp1.1</TargetFramework> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<Folder Include="wwwroot\" /> | ||
</ItemGroup> | ||
<ItemGroup> | ||
<PackageReference Include="Microsoft.AspNetCore" Version="1.1.1" /> | ||
<PackageReference Include="Microsoft.AspNetCore.Mvc" Version="1.1.2" /> | ||
<PackageReference Include="Microsoft.AspNetCore.StaticFiles" Version="1.1.1" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\EPPlus.Core\EPPlus.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
using System.IO; | ||
using Microsoft.AspNetCore.Hosting; | ||
|
||
namespace EPPlus.Core.SampleWebApp | ||
{ | ||
public class Program | ||
{ | ||
public static void Main(string[] args) | ||
{ | ||
var host = new WebHostBuilder() | ||
.UseKestrel() | ||
.UseContentRoot(Directory.GetCurrentDirectory()) | ||
.UseIISIntegration() | ||
.UseStartup<Startup>() | ||
.Build(); | ||
|
||
host.Run(); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
src/EPPlus.Core.SampleWebApp/Properties/launchSettings.json
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
{ | ||
"iisSettings": { | ||
"windowsAuthentication": false, | ||
"anonymousAuthentication": true, | ||
"iisExpress": { | ||
"applicationUrl": "http://localhost:2463/", | ||
"sslPort": 0 | ||
} | ||
}, | ||
"profiles": { | ||
"IIS Express": { | ||
"commandName": "IISExpress", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
} | ||
}, | ||
"EPPlus.Core.SampleWebApp": { | ||
"commandName": "Project", | ||
"launchBrowser": true, | ||
"environmentVariables": { | ||
"ASPNETCORE_ENVIRONMENT": "Development" | ||
}, | ||
"applicationUrl": "http://localhost:2464" | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Microsoft.AspNetCore.Builder; | ||
using Microsoft.AspNetCore.Hosting; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.Extensions.Logging; | ||
|
||
namespace EPPlus.Core.SampleWebApp | ||
{ | ||
public class Startup | ||
{ | ||
public void ConfigureServices(IServiceCollection services) | ||
{ | ||
services.AddMvc(); | ||
} | ||
|
||
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) | ||
{ | ||
loggerFactory.AddConsole(); | ||
|
||
if (env.IsDevelopment()) | ||
{ | ||
app.UseDeveloperExceptionPage(); | ||
} | ||
|
||
app.UseMvcWithDefaultRoute(); | ||
app.UseDefaultFiles(); | ||
app.UseStaticFiles(); | ||
} | ||
} | ||
} |
Binary file not shown.