-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from dimabarbul/52-improve-eppluswriter
- Loading branch information
Showing
17 changed files
with
485 additions
and
188 deletions.
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
.../XReports.Demos/Controllers/EpplusWriterExtensions/CustomEpplusWriterOptionsController.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,84 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using Bogus; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.Extensions.Options; | ||
using XReports.Converter; | ||
using XReports.Excel; | ||
using XReports.Excel.PropertyHandlers; | ||
using XReports.Excel.Writers; | ||
using XReports.ReportCellProperties; | ||
using XReports.SchemaBuilders; | ||
using XReports.Table; | ||
|
||
namespace XReports.Demos.Controllers.EpplusWriterExtensions; | ||
|
||
public class CustomEpplusWriterOptionsController : Controller | ||
{ | ||
private const int RecordsCount = 10; | ||
|
||
public IActionResult Index() | ||
{ | ||
return this.View(); | ||
} | ||
|
||
public IActionResult Download() | ||
{ | ||
IReportTable<ReportCell> reportTable = this.BuildReport(); | ||
IReportTable<ExcelReportCell> excelReportTable = this.ConvertToExcel(reportTable); | ||
|
||
Stream excelStream = this.WriteExcelReportToStream(excelReportTable); | ||
return this.File(excelStream, Constants.ContentTypeExcel, "CustomEpplusWriterOptions.xlsx"); | ||
} | ||
|
||
private IReportTable<ReportCell> BuildReport() | ||
{ | ||
ReportSchemaBuilder<Entity> reportBuilder = new(); | ||
reportBuilder.AddColumn("Name", e => e.Name); | ||
reportBuilder.AddColumn("Score", e => e.Score) | ||
.AddProperties(new DecimalPrecisionProperty(2)); | ||
|
||
IReportTable<ReportCell> reportTable = reportBuilder.BuildVerticalSchema().BuildReportTable(this.GetData()); | ||
return reportTable; | ||
} | ||
|
||
private IReportTable<ExcelReportCell> ConvertToExcel(IReportTable<ReportCell> reportTable) | ||
{ | ||
ReportConverter<ExcelReportCell> excelConverter = new(new[] | ||
{ | ||
new DecimalPrecisionPropertyExcelHandler(), | ||
}); | ||
|
||
return excelConverter.Convert(reportTable); | ||
} | ||
|
||
private Stream WriteExcelReportToStream(IReportTable<ExcelReportCell> reportTable) | ||
{ | ||
EpplusWriter writer = new( | ||
Options.Create(new EpplusWriterOptions() | ||
{ | ||
WorksheetName = "Scores", | ||
StartColumn = 2, | ||
StartRow = 2, | ||
}), | ||
Array.Empty<IEpplusFormatter>()); | ||
|
||
return writer.WriteToStream(reportTable); | ||
} | ||
|
||
private IEnumerable<Entity> GetData() | ||
{ | ||
return new Faker<Entity>() | ||
.RuleFor(e => e.Name, f => f.Name.FullName()) | ||
.RuleFor(e => e.Score, f => f.Random.Decimal(0, 100)) | ||
.Generate(RecordsCount); | ||
} | ||
|
||
private class Entity | ||
{ | ||
public string Name { get; set; } | ||
|
||
public decimal Score { get; set; } | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
demos/XReports.Demos/Views/CustomEpplusWriterOptions/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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
@{ | ||
ViewBag.Title = "Custom EpplusWriter Options"; | ||
Layout = "_DemoPageLayout"; | ||
} | ||
|
||
<a asp-action="Download">Download</a> | ||
|
||
@section Description | ||
{ | ||
<p>By default EpplusWriter writes report to worksheet named "Data" starting at cell A1. This can be changed by using configuring EpplusWriterOptions.</p> | ||
<p>Click Download link to see result.</p> | ||
} | ||
|
||
@section ReportTable | ||
{ | ||
} | ||
|
||
@section Code | ||
{ | ||
EpplusWriter writer = new( | ||
Options.Create(new EpplusWriterOptions() | ||
{ | ||
WorksheetName = "Scores", | ||
StartColumn = 2, | ||
StartRow = 2, | ||
}), | ||
Array.Empty<IEpplusFormatter>()); | ||
} |
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
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
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
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
Oops, something went wrong.