1+ using Syncfusion . XlsIO ;
2+ using System . Globalization ;
3+
4+ namespace FirstUsedCellInUsedRange
5+ {
6+ class Program
7+ {
8+ public static void Main ( string [ ] args )
9+ {
10+
11+ using ( ExcelEngine excelEngine = new ExcelEngine ( ) )
12+ {
13+ IApplication application = excelEngine . Excel ;
14+ application . DefaultVersion = ExcelVersion . Xlsx ;
15+
16+ //Open the input workbook
17+ IWorkbook workbook = application . Workbooks . Open ( Path . GetFullPath ( @"Data/Input.xlsx" ) ) ;
18+
19+ //Access the first worksheet
20+ IWorksheet worksheet = workbook . Worksheets [ 0 ] ;
21+
22+ //Get the used range to iterate through populated cells
23+ IRange used = worksheet . UsedRange ;
24+
25+ //Set culture and parsing styles for interpreting text dates
26+ CultureInfo culture = new CultureInfo ( "en-IN" ) ;
27+ DateTimeStyles styles = DateTimeStyles . None ;
28+
29+ //Iterate through the used range and convert text-formatted dates to DateTime
30+ for ( int row = used . Row ; row <= used . LastRow ; row ++ )
31+ {
32+ for ( int col = used . Column ; col <= used . LastColumn ; col ++ )
33+ {
34+ IRange cell = worksheet [ row , col ] ;
35+ DateTime date ;
36+
37+ //Log if the cell already contains a true DateTime
38+ if ( cell . HasDateTime )
39+ {
40+ Console . WriteLine ( cell . DateTime ) ;
41+ }
42+ //Try parsing text using the specified culture and assign DateTime back to the cell
43+ else if ( DateTime . TryParse ( cell . Value , culture , styles , out date ) )
44+ {
45+ cell . DateTime = date ;
46+ }
47+ }
48+ }
49+
50+ //Saving the workbook
51+ workbook . SaveAs ( Path . GetFullPath ( @"Output/Output.xlsx" ) ) ;
52+ }
53+
54+ }
55+ }
56+ }
0 commit comments