Skip to content

Commit da881e3

Browse files
authored
Merge pull request #292 from SyncfusionExamples/988674-DatetimeUG
UG documentation 988674: FAQ for how to convert text‑formatted date values to DateTime in an Excel workbook
2 parents 2a47eaf + c43827c commit da881e3

5 files changed

Lines changed: 82 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="TextToDateTimeConverter/TextToDateTimeConverter.csproj" />
3+
</Solution>
Binary file not shown.

‎FAQ/DateTime/.NET/TextToDateTimeConverter/TextToDateTimeConverter/Output/.gitkeep‎

Whitespace-only changes.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<None Update="Data\*">
16+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
17+
</None>
18+
<None Update="Output\*">
19+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
20+
</None>
21+
</ItemGroup>
22+
23+
</Project>

0 commit comments

Comments
 (0)