Skip to content
This repository has been archived by the owner on Jan 25, 2019. It is now read-only.

Commit

Permalink
Added a SampleWebApp
Browse files Browse the repository at this point in the history
  • Loading branch information
VahidN committed May 25, 2017
1 parent 8ceab84 commit 4f6f6e2
Show file tree
Hide file tree
Showing 7 changed files with 208 additions and 0 deletions.
7 changes: 7 additions & 0 deletions EPPlus.Core.sln
Expand Up @@ -15,6 +15,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EPPlus.Core", "src\EPPlus.C
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EPPlus.Core.FunctionalTests", "src\EPPlus.Core.FunctionalTests\EPPlus.Core.FunctionalTests.csproj", "{B3F95982-4924-42B0-A23A-D5CA7895F92C}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "EPPlus.Core.SampleWebApp", "src\EPPlus.Core.SampleWebApp\EPPlus.Core.SampleWebApp.csproj", "{A8F18944-DEDA-413B-AEA4-F4FC1227D374}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -29,12 +31,17 @@ Global
{B3F95982-4924-42B0-A23A-D5CA7895F92C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B3F95982-4924-42B0-A23A-D5CA7895F92C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B3F95982-4924-42B0-A23A-D5CA7895F92C}.Release|Any CPU.Build.0 = Release|Any CPU
{A8F18944-DEDA-413B-AEA4-F4FC1227D374}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A8F18944-DEDA-413B-AEA4-F4FC1227D374}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A8F18944-DEDA-413B-AEA4-F4FC1227D374}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A8F18944-DEDA-413B-AEA4-F4FC1227D374}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{C3EB7AEC-CCF7-42B8-B137-0A8AE146FBC1} = {210E96E3-E4A7-41EE-B074-59FCDAE0B1EC}
{B3F95982-4924-42B0-A23A-D5CA7895F92C} = {210E96E3-E4A7-41EE-B074-59FCDAE0B1EC}
{A8F18944-DEDA-413B-AEA4-F4FC1227D374} = {210E96E3-E4A7-41EE-B074-59FCDAE0B1EC}
EndGlobalSection
EndGlobal
105 changes: 105 additions & 0 deletions src/EPPlus.Core.SampleWebApp/Controllers/HomeController.cs
@@ -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 src/EPPlus.Core.SampleWebApp/EPPlus.Core.SampleWebApp.csproj
@@ -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>
20 changes: 20 additions & 0 deletions src/EPPlus.Core.SampleWebApp/Program.cs
@@ -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 src/EPPlus.Core.SampleWebApp/Properties/launchSettings.json
@@ -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"
}
}
}
29 changes: 29 additions & 0 deletions src/EPPlus.Core.SampleWebApp/Startup.cs
@@ -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.

0 comments on commit 4f6f6e2

Please sign in to comment.