-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
113 lines (92 loc) · 4.44 KB
/
Program.cs
File metadata and controls
113 lines (92 loc) · 4.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System.Globalization;
using SharpExcel.Models.Results;
using SharpExcel.Models.Styling.Colorization;
using SharpExcel.TestApplication.TestData;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using SharpExcel.Abstraction;
using SharpExcel.DependencyInjection;
using SharpExcel.Models.Styling;
using SharpExcel.Models.Styling.Constants;
using SharpExcel.Models.Styling.Text;
HostApplicationBuilder builder = Host.CreateEmptyApplicationBuilder(null);
//Here we add a synchronizer to the service collection
builder.Services.AddSharpExcelSynchronizer<TestExportModel>(options =>
{
//set default style of
options.WithDataStyle(ExcelCellStyleConstants.DefaultDataStyle);
options.WithHeaderStyle(new ExcelCellStyle()
.WithTextStyle(TextStyle.Bold | TextStyle.Underlined)
.WithFontSize(18.0));
//here we define the style of an errored cell.
//This is only applicable when we want to return a validated excel file.
//Any cells that have validation errors will have this style
options.WithErrorStyle(
ExcelCellStyleConstants.DefaultDataStyle
.WithTextColor(new ExcelColor(255, 100, 100))
.WithBackgroundColor(new ExcelColor(255, 100, 100, 70))
);
//We can also define rules for styling
options.WithStylingRule(rule =>
{
//define which property we want to check
rule.ForProperty(nameof(TestExportModel.Budget));
//define the condition for this rule
rule.WithCondition(x => x.Budget < 0);
//define style to do when the rule is true
rule.WhenTrue(ExcelCellStyleConstants.DefaultDataStyle.WithTextColor(new(255, 100, 100)));
//define style for when the rule is false
//can be omitted to use default style
rule.WhenFalse(ExcelCellStyleConstants.DefaultDataStyle.WithTextColor(new(80, 160, 80)));
});
options.WithTarget(rule =>
{
rule.WithCondition(x => x.Status != TestStatus.Fired);
rule.WithSheetName("Employees");
rule.WithStartColumn(3);
rule.WithStartRow(3);
});
options.WithTarget(rule =>
{
rule.WithCondition(x => x.Status == TestStatus.Fired);
rule.WithSheetName("Fired");
rule.WithStartRow(3);
rule.WithStartColumn(3);
});
});
using IHost host = builder.Build();
await RunApp(host.Services);
await host.RunAsync();
//this is our test application
async Task RunApp(IServiceProvider services)
{
var exportPath = $"./OutputFolder/TestExport-{Guid.NewGuid()}.xlsx";
var validationExportPath = $"./OutputFolder/ErrorChecked-{Guid.NewGuid()}.xlsx";
var exportService = services.GetRequiredService<ISharpExcelSynchronizer<TestExportModel>>();
using var workbook = await exportService.GenerateWorkbookAsync(CultureInfo.CurrentCulture, TestDataProvider.GetTestData());
workbook.SaveAs(exportPath);
using var errorCheckedWorkbook = await exportService.ValidateAndAnnotateWorkbookAsync(CultureInfo.CurrentCulture, workbook);
errorCheckedWorkbook.SaveAs(validationExportPath);
var importedWorkbook = await exportService.ReadWorkbookAsync(CultureInfo.CurrentCulture, workbook);
#region write_output
foreach (var dataItem in importedWorkbook.Records)
{
WriteOutputRow(dataItem, importedWorkbook);
}
//This method is just here to write the results of the read operation.
void WriteOutputRow(TestExportModel testExportModel, ExcelReadResult<TestExportModel> excelReadResult)
{
Console.WriteLine($"{testExportModel?.Id} | {testExportModel?.FirstName} | {testExportModel?.LastName} | {testExportModel?.Email} | {testExportModel?.Budget} | {testExportModel?.TestDepartment}");
//print validation errors if needed
if (testExportModel != null && excelReadResult.ValidationResults.TryGetValue(testExportModel, out var validationResults))
{
foreach (var validationResult in validationResults.ValidationResults)
{
Console.ForegroundColor = ConsoleColor.Yellow;
Console.WriteLine($"\tValidation Error on row {validationResults.Address.RowNumber} in column {validationResults.Address.ColumnName} ({validationResults.Address.HeaderName}): {validationResult.ErrorMessage}");
Console.ResetColor();
}
}
}
#endregion
}