1+ #region Copyright Syncfusion Inc. 2001-2019.
2+ // Copyright Syncfusion Inc. 2001-2019. All rights reserved.
3+ // Use of this code is subject to the terms of our license.
4+ // A copy of the current license can be obtained at any time by e-mailing
5+ // licensing@syncfusion.com. Any infringement will be prosecuted under
6+ // applicable laws.
7+ #endregion
8+
9+ using Syncfusion . Drawing ;
10+ using Syncfusion . XlsIO ;
11+ using System ;
12+ using System . Collections . Generic ;
13+ using System . Globalization ;
14+ using System . IO ;
15+ using Color = Syncfusion . Drawing . Color ;
16+ using IApplication = Syncfusion . XlsIO . IApplication ;
17+
18+ namespace Blazor_MAUI_Demos . Pages . DocumentProcessing . Excel
19+ {
20+ public class AttendanceTrackerService
21+ {
22+ #region Constants
23+ string [ ] _columnNames ;
24+ private List < EmployeeDetails > _employeeAttendanceList ;
25+ #endregion
26+
27+ /// <summary>
28+ /// Create an Excel document with attendance tracker
29+ /// </summary>
30+ /// <returns>Return the created excel document as stream</returns>
31+ public MemoryStream AttendanceTrackerXlsIO ( )
32+ {
33+ _columnNames = new string [ ] { "Employee Name" , "Supervisor" , "Present Count" , "Leave Count" , "Absent Count" , "Unplanned %" , "Planned %" } ;
34+
35+ AttendanceDetailsGenerator attendanceDetailsGenerator = new AttendanceDetailsGenerator ( ) ;
36+ _employeeAttendanceList = attendanceDetailsGenerator . GetEmployeeAttendanceDetails ( 2019 , 01 ) ;
37+
38+ //New instance of XlsIO is created.[Equivalent to launching Microsoft Excel with no workbooks open].
39+ //The instantiation process consists of two steps.
40+
41+ //Step 1 : Instantiate the spreadsheet creation engine
42+ using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
43+ {
44+ //Step 2 : Instantiate the excel application object
45+ IApplication application = excelEngine . Excel ;
46+ application . EnableIncrementalFormula = true ;
47+
48+ //Set the default version
49+ application . DefaultVersion = ExcelVersion . Excel2016 ;
50+
51+ #region Workbook Initialize
52+ //Creating new workbook
53+ IWorkbook workbook = application . Workbooks . Create ( 1 ) ;
54+ IWorksheet worksheet = workbook . Worksheets [ 0 ] ;
55+
56+ DateTime dateTime = DateTime . Now ;
57+ string monthName = dateTime . ToString ( "MMM" , CultureInfo . InvariantCulture ) ;
58+ worksheet . Name = monthName + "-" + dateTime . Year ;
59+
60+ CreateHeaderRow ( worksheet ) ; //Format header row
61+ FillAttendanceDetails ( worksheet ) ;
62+ ApplyConditionFormatting ( worksheet ) ;
63+
64+ #region Apply Styles
65+ worksheet . Range [ "A1:AL1" ] . RowHeight = 24 ;
66+ worksheet . Range [ "A2:AL31" ] . RowHeight = 20 ;
67+ worksheet . Range [ "A1:B1" ] . ColumnWidth = 20 ;
68+ worksheet . Range [ "C1:G1" ] . ColumnWidth = 16 ;
69+ worksheet . Range [ "H1:AL31" ] . ColumnWidth = 4 ;
70+
71+ worksheet . Range [ "A1:AL31" ] . CellStyle . Font . Bold = true ;
72+ worksheet . Range [ "A1:AL31" ] . CellStyle . Font . Size = 12 ;
73+ worksheet . Range [ "A2:AL31" ] . CellStyle . Font . RGBColor = Color . FromArgb ( 64 , 64 , 64 ) ;
74+ worksheet . Range [ "A1:AL31" ] . CellStyle . VerticalAlignment = ExcelVAlign . VAlignCenter ;
75+
76+ worksheet . Range [ "A1:AL1" ] . CellStyle . Font . Color = ExcelKnownColors . White ;
77+ worksheet . Range [ "A1:AL1" ] . CellStyle . Color = Color . FromArgb ( 58 , 56 , 56 ) ;
78+
79+ worksheet . Range [ "A1:B31" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignLeft ;
80+ worksheet . Range [ "C2:G31" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
81+ worksheet . Range [ "H1:AL31" ] . CellStyle . HorizontalAlignment = ExcelHAlign . HAlignCenter ;
82+
83+ worksheet . Range [ "A2:B31" ] . CellStyle . IndentLevel = 1 ;
84+ worksheet . Range [ "A1:G1" ] . CellStyle . IndentLevel = 1 ;
85+
86+ worksheet . Range [ "A1:AL1" ] . BorderAround ( ExcelLineStyle . Medium , Color . LightGray ) ;
87+ worksheet . Range [ "A1:AL1" ] . BorderInside ( ExcelLineStyle . Medium , Color . LightGray ) ;
88+
89+ worksheet . Range [ "A2:G31" ] . BorderAround ( ExcelLineStyle . Medium , Color . LightGray ) ;
90+ worksheet . Range [ "A2:G31" ] . BorderInside ( ExcelLineStyle . Medium , Color . LightGray ) ;
91+
92+ worksheet . Range [ "H2:AL31" ] . BorderInside ( ExcelLineStyle . Medium , ExcelKnownColors . White ) ;
93+ #endregion
94+ #endregion
95+
96+ //Save the document as a stream and return the stream
97+ MemoryStream stream = new MemoryStream ( ) ;
98+
99+ //Save the created Excel document to MemoryStream
100+ workbook . SaveAs ( stream ) ;
101+ return stream ;
102+
103+ }
104+ }
105+ #region HelperMethods
106+ /// <summary>
107+ /// Apply the conditional format using workbook
108+ /// </summary>
109+ /// <param name="worksheet">worksheet used to get the range and set the conditional formats</param>
110+ private void ApplyConditionFormatting ( IWorksheet worksheet )
111+ {
112+ IConditionalFormats statusCondition = worksheet [ "H2:AL31" ] . ConditionalFormats ;
113+
114+ IConditionalFormat leaveCondition = statusCondition . AddCondition ( ) ;
115+ leaveCondition . FormatType = ExcelCFType . CellValue ;
116+ leaveCondition . Operator = ExcelComparisonOperator . Equal ;
117+ leaveCondition . FirstFormula = "\" L\" " ;
118+ leaveCondition . BackColorRGB = Color . FromArgb ( 253 , 167 , 92 ) ;
119+
120+ IConditionalFormat absentCondition = statusCondition . AddCondition ( ) ;
121+ absentCondition . FormatType = ExcelCFType . CellValue ;
122+ absentCondition . Operator = ExcelComparisonOperator . Equal ;
123+ absentCondition . FirstFormula = "\" A\" " ;
124+ absentCondition . BackColorRGB = Color . FromArgb ( 255 , 105 , 124 ) ;
125+
126+ IConditionalFormat presentCondition = statusCondition . AddCondition ( ) ;
127+ presentCondition . FormatType = ExcelCFType . CellValue ;
128+ presentCondition . Operator = ExcelComparisonOperator . Equal ;
129+ presentCondition . FirstFormula = "\" P\" " ;
130+ presentCondition . BackColorRGB = Color . FromArgb ( 67 , 233 , 123 ) ;
131+
132+ IConditionalFormat weekendCondition = statusCondition . AddCondition ( ) ;
133+ weekendCondition . FormatType = ExcelCFType . CellValue ;
134+ weekendCondition . Operator = ExcelComparisonOperator . Equal ;
135+ weekendCondition . FirstFormula = "\" WE\" " ;
136+ weekendCondition . BackColorRGB = Color . FromArgb ( 240 , 240 , 240 ) ;
137+
138+ IConditionalFormats presentSummaryCF = worksheet [ "C2:C31" ] . ConditionalFormats ;
139+ IConditionalFormat presentCountCF = presentSummaryCF . AddCondition ( ) ;
140+ presentCountCF . FormatType = ExcelCFType . DataBar ;
141+ IDataBar dataBar = presentCountCF . DataBar ;
142+ dataBar . BarColor = Color . FromArgb ( 61 , 242 , 142 ) ;
143+
144+ IConditionalFormats leaveSummaryCF = worksheet [ "D2:D31" ] . ConditionalFormats ;
145+ IConditionalFormat leaveCountCF = leaveSummaryCF . AddCondition ( ) ;
146+ leaveCountCF . FormatType = ExcelCFType . DataBar ;
147+ dataBar = leaveCountCF . DataBar ;
148+ dataBar . BarColor = Color . FromArgb ( 242 , 71 , 23 ) ;
149+
150+ IConditionalFormats absentSummaryCF = worksheet [ "E2:E31" ] . ConditionalFormats ;
151+ IConditionalFormat absentCountCF = absentSummaryCF . AddCondition ( ) ;
152+ absentCountCF . FormatType = ExcelCFType . DataBar ;
153+ dataBar = absentCountCF . DataBar ;
154+ dataBar . BarColor = Color . FromArgb ( 255 , 10 , 69 ) ;
155+
156+ IConditionalFormats unplannedSummaryCF = worksheet [ "F2:F31" ] . ConditionalFormats ;
157+ IConditionalFormat unplannedCountCF = unplannedSummaryCF . AddCondition ( ) ;
158+ unplannedCountCF . FormatType = ExcelCFType . DataBar ;
159+ dataBar = unplannedCountCF . DataBar ;
160+ dataBar . MaxPoint . Type = ConditionValueType . HighestValue ;
161+ dataBar . BarColor = Color . FromArgb ( 142 , 142 , 142 ) ;
162+
163+ IConditionalFormats plannedSummaryCF = worksheet [ "G2:G31" ] . ConditionalFormats ;
164+ IConditionalFormat plannedCountCF = plannedSummaryCF . AddCondition ( ) ;
165+ plannedCountCF . FormatType = ExcelCFType . DataBar ;
166+ dataBar = plannedCountCF . DataBar ;
167+ dataBar . MaxPoint . Type = ConditionValueType . HighestValue ;
168+ dataBar . BarColor = Color . FromArgb ( 56 , 136 , 254 ) ;
169+
170+ }
171+ /// <summary>
172+ /// Used to fill the attendance details
173+ /// </summary>
174+ /// <param name="worksheet">worksheet used to get the range and fill attendance details</param>
175+ private void FillAttendanceDetails ( IWorksheet worksheet )
176+ {
177+ int rowIndex = 2 ;
178+ foreach ( EmployeeDetails empDetails in _employeeAttendanceList )
179+ {
180+
181+ worksheet [ "A" + rowIndex ] . Text = empDetails . Name ;
182+ worksheet [ "B" + rowIndex ] . Text = empDetails . Supervisor ;
183+ for ( int colIndex = 0 ; colIndex < empDetails . Attendances . Count ; colIndex ++ )
184+ {
185+ worksheet [ rowIndex , colIndex + 8 ] . Text = empDetails . Attendances [ colIndex ] ;
186+ }
187+ rowIndex ++ ;
188+ }
189+ //Data validation for list
190+ IDataValidation validation = worksheet . Range [ "H2:AL31" ] . DataValidation ;
191+ validation . ListOfValues = new string [ ] { "P" , "A" , "L" , "WE" } ;
192+
193+ worksheet [ "C2:C31" ] . Formula = "=CountIf('H2:AL2',\" P\" )" ;
194+ worksheet [ "D2:D31" ] . Formula = "=CountIf('H2:AL2',\" L\" )" ;
195+ worksheet [ "E2:E31" ] . Formula = "=CountIf('H2:AL2',\" A\" )" ;
196+ worksheet [ "F2:F31" ] . Formula = "=E2/(C2+D2+E2)" ;
197+ worksheet [ "G2:G31" ] . Formula = "=D2/(C2+D2+E2)" ;
198+ worksheet [ "F2:G31" ] . NumberFormat = ".00 %" ;
199+ }
200+ private void CreateHeaderRow ( IWorksheet worksheet )
201+ {
202+ for ( int i = 0 ; i < _columnNames . Length ; i ++ )
203+ {
204+ worksheet [ 1 , i + 1 ] . Text = _columnNames [ i ] ;
205+ }
206+ worksheet [ "H1" ] . DateTime = new DateTime ( 2019 , 1 , 1 ) ;
207+
208+ worksheet [ "I1:AL1" ] . Formula = "=H1+1" ;
209+ worksheet [ "H1:AL1" ] . NumberFormat = "d" ;
210+ }
211+ #endregion
212+ }
213+ #region HelperClasses
214+ /// <summary>
215+ /// Return the list of employee details
216+ /// </summary>
217+ public class EmployeeDetails
218+ {
219+ public string Name { get ; set ; }
220+ public string Supervisor { get ; set ; }
221+ public List < string > Attendances { get ; set ; }
222+ public EmployeeDetails ( )
223+ {
224+ Attendances = new List < string > ( ) ;
225+ }
226+
227+ }
228+ /// <summary>
229+ /// Get the attendance details and return the list
230+ /// </summary>
231+ public class AttendanceDetailsGenerator
232+ {
233+ private List < EmployeeDetails > _employeeAttendanceList ;
234+ string [ ] _dayStatus ;
235+ string [ ] _supervisor ;
236+ string [ ] _employeeNames ;
237+ public AttendanceDetailsGenerator ( )
238+ {
239+ _employeeAttendanceList = new List < EmployeeDetails > ( ) ;
240+ _dayStatus = new string [ ] { "P" , "L" , "P" , "A" , "P" } ;
241+ _supervisor = new string [ ] { "Mary Saveley" , "Liz Nixon" , "Liu Wong" , "Michael Holz" } ;
242+ _employeeNames = new string [ ] { "Maria Anders" , "Ana Trujillo" , "Antonio Moreno" , "Thomas Hardy" , "Christina Berglund" , "Hanna Moos" ,
243+ "Frederique Citeaux" , "Martin Sommer" , "Laurence Lebihan" , "Elizabeth Lincoln" , "Victoria Ashworth" , "Patricio Simpson" ,
244+ "Francisco Chang" , "Yang Wang" , "Pedro Afonso" , "Elizabeth Brown" , "Steve Rogers" , "Ann Devon" ,
245+ "Philip Cramer" , "Daniel Tonini" , "Annette Roulet" , "John Smith" , "Maria Larsson" , "Howard Stark" ,
246+ "Peter Franken" , "Aria Cruz" , "Philip Gary" , "Fran Willamson" , "Howard Snyde" , "Mario Pontes" } ;
247+ }
248+
249+ public List < EmployeeDetails > GetEmployeeAttendanceDetails ( int year , int month )
250+ {
251+ Random rnd = new Random ( ) ;
252+ for ( int i = 0 ; i < 30 ; i ++ )
253+ {
254+ EmployeeDetails details = new EmployeeDetails ( ) ;
255+ details . Name = _employeeNames [ i ] ;
256+ details . Supervisor = _supervisor [ rnd . Next ( _supervisor . Length ) ] ;
257+ int numberOfDays = DateTime . DaysInMonth ( year , month ) ;
258+ for ( int j = 0 ; j < numberOfDays ; j ++ )
259+ {
260+ DateTime date = new DateTime ( year , month , j + 1 ) ;
261+ if ( date . DayOfWeek == DayOfWeek . Saturday || date . DayOfWeek == DayOfWeek . Sunday )
262+ details . Attendances . Add ( "WE" ) ;
263+ else
264+ details . Attendances . Add ( _dayStatus [ rnd . Next ( _dayStatus . Length ) ] ) ;
265+ }
266+ _employeeAttendanceList . Add ( details ) ;
267+ }
268+ return _employeeAttendanceList ;
269+ }
270+ }
271+ #endregion
272+ }
0 commit comments