diff --git a/exceldatareader/.gitignore b/exceldatareader/.gitignore new file mode 100644 index 00000000..2cb90536 --- /dev/null +++ b/exceldatareader/.gitignore @@ -0,0 +1,5 @@ +bin/ +obj/ +*.sln.iml +.idea/**/* +**/.idea/* \ No newline at end of file diff --git a/exceldatareader/Apps/ExcelDataReaderApp.cs b/exceldatareader/Apps/ExcelDataReaderApp.cs new file mode 100644 index 00000000..29ebd40c --- /dev/null +++ b/exceldatareader/Apps/ExcelDataReaderApp.cs @@ -0,0 +1,169 @@ + +using System.Data; +using System.IO; + +[App(icon: Icons.Sheet)] +public class ExcelDataReaderApp : ViewBase +{ + public record User + { + public string? ID { get; set; } + public string? Name { get; set; } + public string? Email { get; set; } + public string? PhoneNumber { get; set; } + public string? Address { get; set; } + public string? Gender { get; set; } + public string? Department { get; set; } + public string? Level { get; set; } + } + + + /// + /// Applies pagination to a list of users and returns the paginated results along with total page count + /// + /// The current page number (1-based index) + /// Number of items to display per page + /// The complete list of users to paginate + /// + /// A tuple containing: + /// - int: Total number of pages available + /// - List: The subset of users for the requested page + /// + /// + /// + /// var (totalPages, currentPageUsers) = PaginationValue(2, 10, allUsers); + /// // Returns page 2 with 10 users per page + /// + /// + private (int, List) PaginationValue(int page, int totalItem, List users) + { + if (users == null || users.Count == 0) + return (0, new List()); + var totalPage = (int)Math.Ceiling((double)users.Count / totalItem); + // Ensure page is within valid bounds + page = (page >= 1) ? page : 1; + page = (page <= totalPage) ? page : totalPage; + // Return the total pages and the subset of users for the requested page + return (totalPage, users.Skip((page - 1) * totalItem).Take(totalItem).ToList()); + + } + + public override object? Build() + { + // Set Source file url + var FilePath = System.IO.Path.Combine(Directory.GetCurrentDirectory(), "Manpower.csv"); + var filePath = UseState(() => + { + if (!File.Exists(FilePath)) + { + File.WriteAllLines(FilePath, new List + { + "ID,Name,Email,Phone Number,Address,Gender,Department,Level", + "EMP384729,James Smith,j.smith@factory.com,348572918,1234 Industrial Ave,Male,Production,Director", + "EMP529183,Maria Garcia,maria.garcia@factory.com,592837461,5678 Factory St,Female,Quality Control,Manager", + "EMP672819,Robert Johnson,r.johnson@factory.com,672819345,9012 Manufacturing Dr,Male,Warehouse,Team Leader", + }); + return FilePath; + } + return FilePath; + }); + var users = UseState(() => new List()); + var displayUsers = UseState(() => new List()); + var isImport = UseState(false); + var isDelete = UseState(false); + var page = UseState(1); + var totalPage = UseState(0); + var client = UseService(); + + // re-render when users, totalPager, or page change value + UseEffect(() => + { + (totalPage.Value, displayUsers.Value) = PaginationValue(page.Value, 20, users.Value); + }, users, totalPage, page); + + // Load data from "Manpower.csv" file and save them to state variables by click "Import" button or changed filePath link . after finish, re-render page + UseEffect(() => + { + if (isImport.Value) + { + // Read source data file using FileStream and ExcelDataReader to convert data become DataTableCollection type + try + { + Console.WriteLine("Came to file Path: " + filePath); + using var stream = new FileStream(filePath.Value, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); + using var reader = string.Equals(System.IO.Path.GetExtension(filePath.Value), ".csv", StringComparison.OrdinalIgnoreCase) ? + ExcelReaderFactory.CreateCsvReader(stream) : ExcelReaderFactory.CreateReader(stream); + // Use ExcelDataReader.DataSet to convert data to Dataset + DataSet result = reader.AsDataSet(new ExcelDataSetConfiguration() + { + ConfigureDataTable = (_) => new ExcelDataTableConfiguration() + { + UseHeaderRow = true + } + }); + // Convert Data to List + if (result != null && result.Tables.Count > 0) + { + users.Value = result.Tables[0].AsEnumerable() + .Select(u => new User + { + ID = u.Field("ID"), + Name = u.Field("Name"), + Email = u.Field("Email"), + PhoneNumber = u.Field("Phone Number"), + Address = u.Field("Address"), + Gender = u.Field("Gender"), + Department = u.Field("Department"), + Level = u.Field("Level") + }) + .ToList(); + } + else + { + users.Value = new List(); + } + // devide data to display on the screen, using pagination. + (totalPage.Value, displayUsers.Value) = PaginationValue(page.Value, 20, users.Value); + // Reset "Import" button and dislay alert + isImport.Set(false); + client.Toast("Import successfull", "Notification"); + + } + catch (Exception ex) + { + client.Toast($"Import Error: {ex.Message}", "Error"); + } + } + }, isImport, filePath); + // Delete all data + UseEffect(() => + { + users.Value = new List(); + displayUsers.Value = new List(); + isDelete.Set(false); + }, isDelete); + return Layout.Vertical( + Layout.Horizontal().Align(Align.Right) | + Layout.Horizontal(Text.H2("This is the example for Nuget exceldatareader")).Align(Align.Left) | + new Button("Import", _ => + { + isImport.Set(true); + }) + | new Button("Delete", _ => + { + isDelete.Set(true); + }).Destructive() + + ) + | Layout.Vertical( + displayUsers?.Value.Count > 0 ? + new Card( + Layout.Vertical() + | displayUsers?.Value.ToTable().Width(Size.Full()) + | new Pagination(page.Value, totalPage.Value, newPage => page.Set(newPage.Value)) + + ).Title("Employee List").Width(Size.Full()) : Text.Label("No employee") + ); + } +} + diff --git a/exceldatareader/GlobalUsings.cs b/exceldatareader/GlobalUsings.cs new file mode 100644 index 00000000..e6ae17d0 --- /dev/null +++ b/exceldatareader/GlobalUsings.cs @@ -0,0 +1,29 @@ +global using Ivy; +global using Ivy.Apps; +global using Ivy.Auth; +global using Ivy.Chrome; +global using Ivy.Client; +global using Ivy.Core; +global using Ivy.Core.Hooks; +global using Ivy.Helpers; +global using Ivy.Hooks; +global using Ivy.Shared; +global using Ivy.Services; +global using Ivy.Views; +global using Ivy.Views.Alerts; +global using Ivy.Views.Blades; +global using Ivy.Views.Builders; +global using Ivy.Views.Charts; +global using Ivy.Views.Dashboards; +global using Ivy.Views.Forms; +global using Ivy.Views.Tables; +global using Ivy.Widgets.Inputs; +global using Microsoft.Extensions.Configuration; +global using Microsoft.Extensions.DependencyInjection; +global using Microsoft.Extensions.Logging; +global using System.Collections.Immutable; +global using System.ComponentModel.DataAnnotations; +global using System.Globalization; +global using System.Reactive.Linq; +global using ExcelDataReader; +namespace exceldatareaderApp; diff --git a/exceldatareader/Manpower.csv b/exceldatareader/Manpower.csv new file mode 100644 index 00000000..1fdedd63 --- /dev/null +++ b/exceldatareader/Manpower.csv @@ -0,0 +1,91 @@ +ID,Name,Email,Phone Number,Address,Gender,Department,Level +EMP384729,James Smith,j.smith@factory.com,348572918,1234 Industrial Ave,Male,Production,Director +EMP529183,Maria Garcia,maria.garcia@factory.com,592837461,5678 Factory St,Female,Quality Control,Manager +EMP672819,Robert Johnson,r.johnson@factory.com,672819345,9012 Manufacturing Dr,Male,Warehouse,Team Leader +EMP819273,Linda Brown,l.brown@factory.com,819273654,3456 Assembly Ln,Female,Packaging,Staff +EMP192837,Michael Davis,m.davis@factory.com,192837465,7890 Production Blvd,Male,Machining,Supervisor +EMP837291,Jennifer Wilson,j.wilson@factory.com,837291546,2345 Warehouse Rd,Female,Maintenance,Staff +EMP465782,William Martinez,w.martinez@factory.com,465782193,6789 Plant St,Male,Electrical,Team Leader +EMP918273,Patricia Anderson,p.anderson@factory.com,918273645,1235 Machine Dr,Female,Plumbing,Staff +EMP273849,David Taylor,d.taylor@factory.com,273849516,4678 Industrial Ave,Male,Safety,Manager +EMP647382,Susan Thomas,s.thomas@factory.com,647382915,8901 Factory St,Female,Inventory,Staff +EMP384716,Richard Jackson,r.jackson@factory.com,384716295,2346 Manufacturing Dr,Male,Assembly,Director +EMP592837,Jessica White,j.white@factory.com,592837164,5679 Assembly Ln,Female,Quality Control,Team Leader +EMP728194,Charles Harris,c.harris@factory.com,728194635,9013 Production Blvd,Male,Logistics,Supervisor +EMP819465,Karen Martin,k.martin@factory.com,819465372,3457 Warehouse Rd,Female,Packaging,Staff +EMP192748,Daniel Thompson,d.thompson@factory.com,192748563,7891 Plant St,Male,Machining,Manager +EMP837192,Lisa Garcia,l.garcia@factory.com,837192456,2346 Machine Dr,Female,Maintenance,Team Leader +EMP465918,Matthew Rodriguez,m.rodriguez@factory.com,465918327,6780 Industrial Ave,Male,Electrical,Staff +EMP918364,Betty Lewis,b.lewis@factory.com,918364275,1236 Factory St,Female,Plumbing,Staff +EMP273495,Anthony Lee,a.lee@factory.com,273495618,4679 Manufacturing Dr,Male,Safety,VP +EMP647293,Donna Walker,d.walker@factory.com,647293184,8902 Assembly Ln,Female,Inventory,Supervisor +EMP384957,Mark Hall,m.hall@factory.com,384957261,2347 Production Blvd,Male,Production,Team Leader +EMP592468,Emily Allen,e.allen@factory.com,592468739,5680 Warehouse Rd,Female,Quality Control,Staff +EMP728351,Donald Young,d.young@factory.com,728351492,9014 Plant St,Male,Logistics,Manager +EMP819576,Michelle King,m.king@factory.com,819576843,3458 Machine Dr,Female,Packaging,Team Leader +EMP192639,Paul Wright,p.wright@factory.com,192639574,7892 Industrial Ave,Male,Machining,Staff +EMP837485,Amanda Scott,a.scott@factory.com,837485196,2347 Factory St,Female,Maintenance,Supervisor +EMP465139,Steven Green,s.green@factory.com,465139728,6781 Manufacturing Dr,Male,Electrical,Director +EMP918752,Melissa Baker,m.baker@factory.com,918752364,1237 Assembly Ln,Female,Plumbing,Manager +EMP273684,Joshua Adams,j.adams@factory.com,273684957,4680 Production Blvd,Male,Safety,Team Leader +EMP647815,Deborah Nelson,d.nelson@factory.com,647815239,8903 Warehouse Rd,Female,Inventory,Staff +EMP385729,Andrew Carter,a.carter@factory.com,385729461,2348 Plant St,Male,Production,Staff +EMP592183,Rebecca Mitchell,r.mitchell@factory.com,592183754,5681 Machine Dr,Female,Quality Control,Supervisor +EMP728492,Jason Perez,j.perez@factory.com,728492315,9015 Industrial Ave,Male,Logistics,Team Leader +EMP819637,Sarah Roberts,s.roberts@factory.com,819637482,3459 Factory St,Female,Packaging,Manager +EMP192846,Kevin Turner,k.turner@factory.com,192846573,7893 Manufacturing Dr,Male,Machining,Director +EMP837591,Carol Phillips,c.phillips@factory.com,837591246,2348 Assembly Ln,Female,Maintenance,Staff +EMP465382,Brian Campbell,b.campbell@factory.com,465382917,6782 Production Blvd,Male,Electrical,Team Leader +EMP918463,Dorothy Parker,d.parker@factory.com,918463725,1238 Warehouse Rd,Female,Plumbing,Staff +EMP273759,Edward Evans,e.evans@factory.com,273759684,4681 Plant St,Male,Safety,Supervisor +EMP647928,Nancy Edwards,n.edwards@factory.com,647928351,8904 Machine Dr,Female,Inventory,Manager +EMP385164,Ronald Collins,r.collins@factory.com,385164792,2349 Industrial Ave,Male,Production,Supervisor +EMP592739,Sandra Stewart,s.stewart@factory.com,592739481,5682 Factory St,Female,Quality Control,Team Leader +EMP728615,Thomas Sanchez,t.sanchez@factory.com,728615394,9016 Manufacturing Dr,Male,Logistics,Staff +EMP819742,Margaret Morris,m.morris@factory.com,819742563,3460 Assembly Ln,Female,Packaging,Staff +EMP192935,Christopher Rogers,c.rogers@factory.com,192935478,7894 Production Blvd,Male,Machining,Team Leader +EMP837684,Sharon Reed,s.reed@factory.com,837684125,2349 Warehouse Rd,Female,Maintenance,Manager +EMP465597,Daniel Cook,d.cook@factory.com,465597832,6783 Plant St,Male,Electrical,Supervisor +EMP918836,Laura Morgan,l.morgan@factory.com,918836749,1239 Machine Dr,Female,Plumbing,Team Leader +EMP273198,George Bell,g.bell@factory.com,273198567,4682 Industrial Ave,Male,Safety,Staff +EMP647351,Kathleen Murphy,k.murphy@factory.com,647351928,8905 Factory St,Female,Inventory,Director +EMP385492,Donald Bailey,d.bailey@factory.com,385492716,2350 Manufacturing Dr,Male,Production,Manager +EMP592615,Angela Rivera,a.rivera@factory.com,592615387,5683 Assembly Ln,Female,Quality Control,Staff +EMP728739,Paul Cooper,p.cooper@factory.com,728739254,9017 Production Blvd,Male,Logistics,Director +EMP819856,Ruth Richardson,r.richardson@factory.com,819856473,3461 Warehouse Rd,Female,Packaging,Supervisor +EMP193147,Mark Cox,m.cox@factory.com,193147586,7895 Plant St,Male,Machining,Staff +EMP837792,Emma Howard,e.howard@factory.com,837792314,2350 Machine Dr,Female,Maintenance,Team Leader +EMP465819,Kenneth Ward,k.ward@factory.com,465819237,6784 Industrial Ave,Male,Electrical,Manager +EMP918947,Alice Torres,a.torres@factory.com,918947562,1240 Factory St,Female,Plumbing,Staff +EMP273264,Steven Peterson,s.peterson@factory.com,273264879,4683 Manufacturing Dr,Male,Safety,Team Leader +EMP647483,Helen Gray,h.gray@factory.com,647483192,8906 Assembly Ln,Female,Inventory,Supervisor +EMP385576,Gary Ramirez,g.ramirez@factory.com,385576921,2351 Production Blvd,Male,Production,Staff +EMP592897,Debra James,d.james@factory.com,592897634,5684 Warehouse Rd,Female,Quality Control,Manager +EMP728918,Scott Watson,s.watson@factory.com,728918745,9018 Plant St,Male,Logistics,Team Leader +EMP819369,Judy Brooks,j.brooks@factory.com,819369258,3462 Machine Dr,Female,Packaging,Staff +EMP193258,Eric Kelly,e.kelly@factory.com,193258467,7896 Industrial Ave,Male,Machining,Supervisor +EMP837415,Michelle Sanders,m.sanders@factory.com,837415986,2351 Factory St,Female,Maintenance,Staff +EMP465638,Stephen Price,s.price@factory.com,465638719,6785 Manufacturing Dr,Male,Electrical,Team Leader +EMP918259,Donna Bennett,d.bennett@factory.com,918259374,1241 Assembly Ln,Female,Plumbing,Manager +EMP273471,Timothy Wood,t.wood@factory.com,273471685,4684 Production Blvd,Male,Safety,Director +EMP647592,Carolyn Barnes,c.barnes@factory.com,647592813,8907 Warehouse Rd,Female,Inventory,Team Leader +EMP385693,Jose Ross,j.ross@factory.com,385693742,2352 Plant St,Male,Production,Team Leader +EMP592724,Amanda Henderson,a.henderson@factory.com,592724139,5685 Machine Dr,Female,Quality Control,Staff +EMP728835,Raymond Coleman,r.coleman@factory.com,728835964,9019 Industrial Ave,Male,Logistics,Staff +EMP819947,Katherine Jenkins,k.jenkins@factory.com,819947352,3463 Factory St,Female,Packaging,Director +EMP193369,Jeffrey Perry,j.perry@factory.com,193369478,7897 Manufacturing Dr,Male,Machining,Team Leader +EMP837526,Lisa Powell,l.powell@factory.com,837526891,2352 Assembly Ln,Female,Maintenance,Supervisor +EMP465759,Ryan Long,r.long@factory.com,465759328,6786 Production Blvd,Male,Electrical,Staff +EMP918368,Martha Flores,m.flores@factory.com,918368457,1242 Warehouse Rd,Female,Plumbing,Team Leader +EMP273582,Jacob Patterson,j.patterson@factory.com,273582619,4685 Plant St,Male,Safety,Manager +EMP647715,Jean Washington,j.washington@factory.com,647715283,8908 Machine Dr,Female,Inventory,Staff +EMP385827,Adam Butler,a.butler@factory.com,385827496,2353 Industrial Ave,Male,Production,Manager +EMP592938,Victoria Simmons,v.simmons@factory.com,592938174,5686 Factory St,Female,Quality Control,Team Leader +EMP728149,Patrick Foster,p.foster@factory.com,728149635,9020 Manufacturing Dr,Male,Logistics,Supervisor +EMP819258,Brenda Gonzales,b.gonzales@factory.com,819258746,3464 Assembly Ln,Female,Packaging,Staff +EMP193471,Jerry Bryant,j.bryant@factory.com,193471582,7898 Production Blvd,Male,Machining,Manager +EMP837639,Rachel Alexander,r.alexander@factory.com,837639415,2353 Warehouse Rd,Female,Maintenance,Director +EMP465872,Henry Russell,h.russell@factory.com,465872931,6787 Plant St,Male,Electrical,VP +EMP918483,Julia Griffin,j.griffin@factory.com,918483267,1243 Machine Dr,Female,Plumbing,Staff +EMP273694,Jack Diaz,j.diaz@factory.com,273694718,4686 Industrial Ave,Male,Safety,Team Leader +EMP647827,Megan Hayes,m.hayes@factory.com,647827359,8909 Factory St,Female,Inventory,Supervisor \ No newline at end of file diff --git a/exceldatareader/Program.cs b/exceldatareader/Program.cs new file mode 100644 index 00000000..fec59eba --- /dev/null +++ b/exceldatareader/Program.cs @@ -0,0 +1,12 @@ + +CultureInfo.DefaultThreadCurrentCulture = CultureInfo.DefaultThreadCurrentUICulture = new CultureInfo("en-US"); +System.Text.Encoding.RegisterProvider(System.Text.CodePagesEncodingProvider.Instance); +var server = new Server(); +#if DEBUG +server.UseHotReload(); +#endif +server.AddAppsFromAssembly(); +server.AddConnectionsFromAssembly(); +var chromeSettings = new ChromeSettings().UseTabs(preventDuplicates: true); +server.UseChrome(chromeSettings); +await server.RunAsync(); diff --git a/exceldatareader/README.md b/exceldatareader/README.md new file mode 100644 index 00000000..a292557e --- /dev/null +++ b/exceldatareader/README.md @@ -0,0 +1,17 @@ +# exceldatareaderApp + +Web application created using [Ivy](https://github.com/Ivy-Interactive/Ivy). + +Ivy is a web framework for building interactive web applications using C# and .NET. + +## Run + +``` +dotnet watch +``` + +## Deploy + +``` +ivy deploy +``` \ No newline at end of file diff --git a/exceldatareader/Screenshots/exceldatareader_Import_press.png b/exceldatareader/Screenshots/exceldatareader_Import_press.png new file mode 100644 index 00000000..437746f3 Binary files /dev/null and b/exceldatareader/Screenshots/exceldatareader_Import_press.png differ diff --git a/exceldatareader/Screenshots/exceldatareader_delete.png b/exceldatareader/Screenshots/exceldatareader_delete.png new file mode 100644 index 00000000..017bafc7 Binary files /dev/null and b/exceldatareader/Screenshots/exceldatareader_delete.png differ diff --git a/exceldatareader/exceldatareader.sln b/exceldatareader/exceldatareader.sln new file mode 100644 index 00000000..8452b74b --- /dev/null +++ b/exceldatareader/exceldatareader.sln @@ -0,0 +1,24 @@ +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.5.2.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "exceldatareaderApp", "exceldatareaderApp.csproj", "{94FD22DC-4066-AF7B-F559-E63DED15E74D}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {94FD22DC-4066-AF7B-F559-E63DED15E74D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {94FD22DC-4066-AF7B-F559-E63DED15E74D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {94FD22DC-4066-AF7B-F559-E63DED15E74D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {94FD22DC-4066-AF7B-F559-E63DED15E74D}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection + GlobalSection(ExtensibilityGlobals) = postSolution + SolutionGuid = {904256BD-A7AB-4A39-9B44-FB850CE512FF} + EndGlobalSection +EndGlobal diff --git a/exceldatareader/exceldatareaderApp.csproj b/exceldatareader/exceldatareaderApp.csproj new file mode 100644 index 00000000..85340363 --- /dev/null +++ b/exceldatareader/exceldatareaderApp.csproj @@ -0,0 +1,30 @@ + + + + Exe + net9.0 + enable + enable + CS8618;CS8603;CS8602;CS8604;CS9113 + exceldatareaderApp + + + + + + + + + + + + + + + + + + + + +