Skip to content

Commit e195cfe

Browse files
authored
Merge pull request #171 from SyncfusionExamples/912298-SetAndFormatTime
912298-Add FAQ for how to set and format time values in Excel using Timespan
2 parents 76a522f + c94cfa2 commit e195cfe

File tree

5 files changed

+131
-0
lines changed

5 files changed

+131
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
# How to set and format time values in Excel using TimeSpan?
2+
3+
Step 1: Create a New C# Console Application Project.
4+
5+
Step 2: Name the Project.
6+
7+
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).
8+
9+
Step 4: Include the following namespaces in the **Program.cs** file.
10+
11+
```csharp
12+
using System;
13+
using System.IO;
14+
using Syncfusion.XlsIO;
15+
```
16+
17+
Step 5: Include the below code snippet in **Program.cs** to set and format time values in Excel using TimeSpan.
18+
19+
```csharp
20+
using (ExcelEngine excelEngine = new ExcelEngine())
21+
{
22+
IApplication application = excelEngine.Excel;
23+
application.DefaultVersion = ExcelVersion.Xlsx;
24+
IWorkbook workbook = application.Workbooks.Create(1);
25+
IWorksheet sheet = workbook.Worksheets[0];
26+
27+
TimeSpan ts = new TimeSpan(12, 32, 38);
28+
29+
//Convert the TimeSpan to a fractional day value that Excel understands
30+
double excelTimeValue = ts.TotalDays;
31+
32+
//Set value in cell
33+
sheet.SetValueRowCol(excelTimeValue, 1, 1);
34+
35+
//Apply the time format to the cell to display it as 'hh:mm:ss'
36+
sheet.Range[1, 1].NumberFormat = "hh:mm:ss";
37+
38+
#region Save
39+
//Saving the workbook
40+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
41+
workbook.SaveAs(outputStream);
42+
#endregion
43+
44+
//Dispose streams
45+
outputStream.Dispose();
46+
}
47+
```
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.12.35506.116 d17.12
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
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}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{AB334E92-14FF-41AB-9D67-88BF1143B550}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{AB334E92-14FF-41AB-9D67-88BF1143B550}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{AB334E92-14FF-41AB-9D67-88BF1143B550}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{AB334E92-14FF-41AB-9D67-88BF1143B550}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
EndGlobal

FAQ/Timespan/.NET/Set and format time values/Set and format time values/Output/.gitkeep

Whitespace-only changes.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.IO;
3+
using Syncfusion.XlsIO;
4+
5+
namespace SetAndFormatTimeValues
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
using (ExcelEngine excelEngine = new ExcelEngine())
12+
{
13+
IApplication application = excelEngine.Excel;
14+
application.DefaultVersion = ExcelVersion.Xlsx;
15+
IWorkbook workbook = application.Workbooks.Create(1);
16+
IWorksheet sheet = workbook.Worksheets[0];
17+
18+
TimeSpan ts = new TimeSpan(12, 32, 38);
19+
20+
//Convert the TimeSpan to a fractional day value that Excel understands
21+
double excelTimeValue = ts.TotalDays;
22+
23+
//Set value in cell
24+
sheet.SetValueRowCol(excelTimeValue, 1, 1);
25+
26+
//Apply the time format to the cell to display it as 'hh:mm:ss'
27+
sheet.Range[1, 1].NumberFormat = "hh:mm:ss";
28+
29+
#region Save
30+
//Saving the workbook
31+
FileStream outputStream = new FileStream(Path.GetFullPath("Output/Output.xlsx"), FileMode.Create, FileAccess.Write);
32+
workbook.SaveAs(outputStream);
33+
#endregion
34+
35+
//Dispose streams
36+
outputStream.Dispose();
37+
}
38+
}
39+
}
40+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<RootNamespace>Set_and_format_time_values</RootNamespace>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
13+
</ItemGroup>
14+
15+
<ItemGroup>
16+
<None Update="Output\*">
17+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
21+
</Project>
22+

0 commit comments

Comments
 (0)