From c94cfa2829f3195406afce01ff8e0b4318b69cf3 Mon Sep 17 00:00:00 2001 From: KarthikaSF4773 Date: Tue, 22 Apr 2025 21:11:05 +0530 Subject: [PATCH] 912298-SetAndFormatTime --- .../.NET/Set and format time values/README.md | 47 +++++++++++++++++++ .../Set and format time values.sln | 22 +++++++++ .../Output/.gitkeep | 0 .../Set and format time values/Program.cs | 40 ++++++++++++++++ .../Set and format time values.csproj | 22 +++++++++ 5 files changed, 131 insertions(+) create mode 100644 FAQ/Timespan/.NET/Set and format time values/README.md create mode 100644 FAQ/Timespan/.NET/Set and format time values/Set and format time values.sln create mode 100644 FAQ/Timespan/.NET/Set and format time values/Set and format time values/Output/.gitkeep create mode 100644 FAQ/Timespan/.NET/Set and format time values/Set and format time values/Program.cs create mode 100644 FAQ/Timespan/.NET/Set and format time values/Set and format time values/Set and format time values.csproj diff --git a/FAQ/Timespan/.NET/Set and format time values/README.md b/FAQ/Timespan/.NET/Set and format time values/README.md new file mode 100644 index 00000000..6f2feb1c --- /dev/null +++ b/FAQ/Timespan/.NET/Set and format time values/README.md @@ -0,0 +1,47 @@ +# How to set and format time values in Excel using TimeSpan? + +Step 1: Create a New C# Console Application Project. + +Step 2: Name the Project. + +Step 3: Install the [Syncfusion.XlsIO.Net.Core](https://www.nuget.org/packages/Syncfusion.XlsIO.Net.Core) NuGet package as reference to your .NET Standard applications from [NuGet.org](https://www.nuget.org). + +Step 4: Include the following namespaces in the **Program.cs** file. + +```csharp +using System; +using System.IO; +using Syncfusion.XlsIO; +``` + +Step 5: Include the below code snippet in **Program.cs** to set and format time values in Excel using TimeSpan. + +```csharp +using (ExcelEngine excelEngine = new ExcelEngine()) +{ + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet sheet = workbook.Worksheets[0]; + + TimeSpan ts = new TimeSpan(12, 32, 38); + + //Convert the TimeSpan to a fractional day value that Excel understands + double excelTimeValue = ts.TotalDays; + + //Set value in cell + sheet.SetValueRowCol(excelTimeValue, 1, 1); + + //Apply the time format to the cell to display it as 'hh:mm:ss' + sheet.Range[1, 1].NumberFormat = "hh:mm:ss"; + + #region Save + //Saving the workbook + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + #endregion + + //Dispose streams + outputStream.Dispose(); +} +``` diff --git a/FAQ/Timespan/.NET/Set and format time values/Set and format time values.sln b/FAQ/Timespan/.NET/Set and format time values/Set and format time values.sln new file mode 100644 index 00000000..188965e5 --- /dev/null +++ b/FAQ/Timespan/.NET/Set and format time values/Set and format time values.sln @@ -0,0 +1,22 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio Version 17 +VisualStudioVersion = 17.12.35506.116 d17.12 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Set and format time values", "Set and format time values\Set and format time values.csproj", "{AB334E92-14FF-41AB-9D67-88BF1143B550}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {AB334E92-14FF-41AB-9D67-88BF1143B550}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {AB334E92-14FF-41AB-9D67-88BF1143B550}.Debug|Any CPU.Build.0 = Debug|Any CPU + {AB334E92-14FF-41AB-9D67-88BF1143B550}.Release|Any CPU.ActiveCfg = Release|Any CPU + {AB334E92-14FF-41AB-9D67-88BF1143B550}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Output/.gitkeep b/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Output/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Program.cs b/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Program.cs new file mode 100644 index 00000000..1eed5b47 --- /dev/null +++ b/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Program.cs @@ -0,0 +1,40 @@ +using System; +using System.IO; +using Syncfusion.XlsIO; + +namespace SetAndFormatTimeValues +{ + class Program + { + static void Main(string[] args) + { + using (ExcelEngine excelEngine = new ExcelEngine()) + { + IApplication application = excelEngine.Excel; + application.DefaultVersion = ExcelVersion.Xlsx; + IWorkbook workbook = application.Workbooks.Create(1); + IWorksheet sheet = workbook.Worksheets[0]; + + TimeSpan ts = new TimeSpan(12, 32, 38); + + //Convert the TimeSpan to a fractional day value that Excel understands + double excelTimeValue = ts.TotalDays; + + //Set value in cell + sheet.SetValueRowCol(excelTimeValue, 1, 1); + + //Apply the time format to the cell to display it as 'hh:mm:ss' + sheet.Range[1, 1].NumberFormat = "hh:mm:ss"; + + #region Save + //Saving the workbook + FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write); + workbook.SaveAs(outputStream); + #endregion + + //Dispose streams + outputStream.Dispose(); + } + } + } +} \ No newline at end of file diff --git a/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Set and format time values.csproj b/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Set and format time values.csproj new file mode 100644 index 00000000..45d41ef7 --- /dev/null +++ b/FAQ/Timespan/.NET/Set and format time values/Set and format time values/Set and format time values.csproj @@ -0,0 +1,22 @@ + + + + Exe + net8.0 + Set_and_format_time_values + enable + enable + + + + + + + + + Always + + + + +