From 61dfc9b2a23894cd3a043330c2f261dee9016944 Mon Sep 17 00:00:00 2001 From: Ricardo Coelho Date: Tue, 12 Dec 2023 23:36:28 -0300 Subject: [PATCH] Fill object 0.5.1 --- README.md | 7 +++---- examples/Program.cs | 16 +++++++++------- src/BlackDigital.Report.csproj | 6 +++--- src/ReportHelper.cs | 20 +++++++++----------- src/Spreadsheet/TableBuilder.cs | 27 +++++++++++++++++++++++++++ 5 files changed, 51 insertions(+), 25 deletions(-) diff --git a/README.md b/README.md index 62d947f..da68b5e 100644 --- a/README.md +++ b/README.md @@ -84,8 +84,7 @@ dotnet add package BlackDigital.Report ReportGenerator.Spreadsheet() .AddSheet("First") .AddTable("Data") - .AddHeader(new List() { "Name", "Number", "ObjDate", "Time" }) - .AddBody(list) + .Fill(list) .BuildAsync(@"test.xlsx"); ``` @@ -188,8 +187,8 @@ The return type of BuildAsync has also changed. Instead of returning just a byte ☑️ Spreadsheet cell configurations. (0.5.0) ☑️ ReportFile class as build return. (0.5.0) ☑️ .NET7 and .NET8 support (0.5.0) - ◼️ Header Globalization. (removed on refactory 0.5.0) - ◼️ Use DisplayAttribute to get name of columns and properties that should be generated. + ☑️ Header Globalization. (0.5.1) + ☑️ Use DisplayAttribute to get name of columns and properties that should be generated. (0.5.1) ◼️ Cells with formulas. ◼️ Cell value event. ◼️ Tables footers. diff --git a/examples/Program.cs b/examples/Program.cs index 357d46b..c5033a3 100644 --- a/examples/Program.cs +++ b/examples/Program.cs @@ -21,6 +21,7 @@ list.Add(new("Line <1> \r\n &A", 10, DateTime.Today, TimeSpan.FromHours(3), DateOnly.FromDateTime(DateTime.Today), new(3, 0))); list.Add(new("Line <2> \r\n &B", -10, DateTime.Now, TimeSpan.FromMinutes(12), DateOnly.FromDateTime(DateTime.Today), new(5, 30))); list.Add(new("Line <3> \n\r &C", 10.6d, DateTime.UtcNow, TimeSpan.FromMinutes(45).Add(TimeSpan.FromSeconds(31)), DateOnly.FromDateTime(DateTime.Today), new(17, 45))); +list.Add(new(null, 10.6d, DateTime.UtcNow, TimeSpan.FromMinutes(45).Add(TimeSpan.FromSeconds(31)), DateOnly.FromDateTime(DateTime.Today), new(17, 45))); List headers = new() { @@ -73,7 +74,8 @@ //System.Text.Json.Serialization.JsonStringEnumConverter //string a; -var task = ReportGenerator.Spreadsheet() +/*var task = ReportGenerator.Spreadsheet() + .SetCompany("BlackDigital") .SetApplication("BlackDigital Report") .SetManager("TioRAC Lab") @@ -106,16 +108,16 @@ task.Wait(); -return; +return;*/ -/*task = ReportGenerator.Spreadsheet() +var task = ReportGenerator.Spreadsheet() .SetCompany("BlackDigital") .SetResourceManager(Texts.ResourceManager) .SetFormatProvider(new CultureInfo("pt")) .AddSheet("First") .AddTable("Data") - .FillObject(list) - .Spreadsheet() + .Fill(list) + /*.Spreadsheet() .AddSheet("Second") .AddValue("My text header") .AddTable("Data2", "B3") @@ -123,7 +125,7 @@ .Fill(list2) .Sheet() .AddTable("Data3", "g4") - .FillObject(list) - .BuildAsync("test-pt.xlsx");*/ + .FillObject(list)*/ + .BuildAsync("D:\\Teste\\temp\\test-default.xlsx"); task.Wait(); \ No newline at end of file diff --git a/src/BlackDigital.Report.csproj b/src/BlackDigital.Report.csproj index 1eacd91..d914a87 100644 --- a/src/BlackDigital.Report.csproj +++ b/src/BlackDigital.Report.csproj @@ -3,11 +3,11 @@ net8.0;net7.0;net6.0;net5.0 enable - 0.5.0 - 0.5.0 + 0.5.1 + 0.5.1 README.md True - $(VersionPrefix)0.5.0 + $(VersionPrefix)0.5.1 Simple report generator using OpenXML. Currently it is only generating reports in spreadsheets. https://github.com/blackdigital-br/BlackReport https://github.com/blackdigital-br/BlackReport diff --git a/src/ReportHelper.cs b/src/ReportHelper.cs index 478cae8..aa417ab 100644 --- a/src/ReportHelper.cs +++ b/src/ReportHelper.cs @@ -4,15 +4,14 @@ using System.Reflection; using System.Globalization; using System.Collections.Generic; -using System.ComponentModel.DataAnnotations; using BlackDigital.Report.Sources; - +using System.ComponentModel.DataAnnotations; namespace BlackDigital.Report { internal static class ReportHelper { - internal static EnumerableReportSource GetObjectHeader(ResourceManager? resource, CultureInfo? culture) + internal static ReportSource GetObjectHeader(ResourceManager? resource, CultureInfo? culture) { var properties = GetPropertiesAndAttributes(); var header = properties.Select(p => @@ -28,18 +27,15 @@ internal static EnumerableReportSource GetObjectHeader(ResourceManager? resou } return columnName; - }).AsEnumerable(); + }).ToList(); - List> headerDataset = new(); - headerDataset.Add(header); - - var source = new EnumerableReportSource(); - source.Load(headerDataset); + var source = new ListReportSource(); + source.Load(header); return source; } - internal static List> ObjectToData(IEnumerable data) + internal static ReportSource ObjectToData(IEnumerable data) { var list = new List>(); @@ -57,8 +53,10 @@ internal static List> ObjectToData(IEnumerable data) list.Add(dataRow); } + var source = new EnumerableReportSource(); + source.Load(list); - return list; + return source; } internal static List> GetPropertiesAndAttributes() diff --git a/src/Spreadsheet/TableBuilder.cs b/src/Spreadsheet/TableBuilder.cs index 8126ef7..62a7f1f 100644 --- a/src/Spreadsheet/TableBuilder.cs +++ b/src/Spreadsheet/TableBuilder.cs @@ -1,5 +1,7 @@ using System.IO; +using System.Globalization; using System.Threading.Tasks; +using System.Collections.Generic; namespace BlackDigital.Report.Spreadsheet { @@ -73,6 +75,17 @@ public TableBuilder AddHeader(ReportSource source) public TableBuilder AddHeader(T data) => AddHeader(Configuration.Sources.FindSource(data)); + public TableBuilder AddHeader() + { + var resource = WorkbookBuilder.Resource; + CultureInfo? culture = null; + + if (WorkbookBuilder.FormatProvider is CultureInfo) + culture = (CultureInfo)WorkbookBuilder.FormatProvider; + + return AddHeader(ReportHelper.GetObjectHeader(resource, culture)); + } + public TableBuilder AddBody(ReportSource source) { if (HasData) @@ -88,6 +101,20 @@ public TableBuilder AddBody(ReportSource source) public TableBuilder AddBody(T data) => AddBody(Configuration.Sources.FindSource(data)); + public TableBuilder Fill(IEnumerable data) + { + var resource = WorkbookBuilder.Resource; + CultureInfo? culture = null; + + if (WorkbookBuilder.FormatProvider is CultureInfo) + culture = (CultureInfo)WorkbookBuilder.FormatProvider; + + AddHeader(ReportHelper.GetObjectHeader(resource, culture)); + AddBody(ReportHelper.ObjectToData(data)); + + return this; + } + #endregion "Builder" #region "Generator"