Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions FAQ/Timespan/.NET/Set and format time values/README.md
Original file line number Diff line number Diff line change
@@ -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();
}
```
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Set_and_format_time_values</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Syncfusion.XlsIO.Net.Core" Version="*" />
</ItemGroup>

<ItemGroup>
<None Update="Output\*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</Project>