diff --git a/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter.slnx b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter.slnx new file mode 100644 index 00000000..3a18fb34 --- /dev/null +++ b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter.slnx @@ -0,0 +1,3 @@ + + + diff --git a/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Data/Input.xlsx b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Data/Input.xlsx new file mode 100644 index 00000000..bf6aa467 Binary files /dev/null and b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Data/Input.xlsx differ diff --git a/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Output/.gitkeep b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Program.cs b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Program.cs new file mode 100644 index 00000000..73a1ebea --- /dev/null +++ b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Program.cs @@ -0,0 +1,56 @@ +using Syncfusion.XlsIO; +using System.Globalization; + +namespace FirstUsedCellInUsedRange +{ + class Program + { + public static void Main(string[] args) + { + + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + + //Open the input workbook + IWorkbook workbook = application.Workbooks.Open(Path.GetFullPath(@"Data/Input.xlsx")); + + //Access the first worksheet + IWorksheet worksheet = workbook.Worksheets[0]; + + //Get the used range to iterate through populated cells + IRange used = worksheet.UsedRange; + + //Set culture and parsing styles for interpreting text dates + CultureInfo culture = new CultureInfo("en-IN"); + DateTimeStyles styles = DateTimeStyles.None; + + //Iterate through the used range and convert text-formatted dates to DateTime + for (int row = used.Row; row <= used.LastRow; row++) + { + for (int col = used.Column; col <= used.LastColumn; col++) + { + IRange cell = worksheet[row, col]; + DateTime date; + + //Log if the cell already contains a true DateTime + if (cell.HasDateTime) + { + Console.WriteLine(cell.DateTime); + } + //Try parsing text using the specified culture and assign DateTime back to the cell + else if (DateTime.TryParse(cell.Value, culture, styles, out date)) + { + cell.DateTime = date; + } + } + } + + //Saving the workbook + workbook.SaveAs(Path.GetFullPath(@"Output/Output.xlsx")); + } + + } + } +} \ No newline at end of file diff --git a/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/TextToDateTimeConverter.csproj b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/TextToDateTimeConverter.csproj new file mode 100644 index 00000000..acbfb2e4 --- /dev/null +++ b/FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/TextToDateTimeConverter.csproj @@ -0,0 +1,23 @@ + + + + Exe + net10.0 + enable + enable + + + + + + + + + Always + + + Always + + + +