Setting the display formats of date time values in cell is a wonderful feature of Microsoft Excel. Add date time data can be used to represent different values including year, month, day, hour, minute or second values etc. All these date time values are displayed in different formats depending on the type of information it represents. XsExcel SDK supports this functionality and allows developers to set any customized display format for a date in C# programming.
This guide page provides C# users with how to format date time in cell in spreadsheet using XsExcel control for C#.NET.
// Create a new excel workbook Workbook workBook = new Workbook(); // Create a new worksheet from workbook Sheet sheet = workBook.CreateSheet(); // Create a new row at first line Row row = sheet.CreateRow(0); // Create a date time cell Cell cellDateTime = row.CreateCell(0); cellDateTime.SetValue(DateTime.Now, "dd-MM-yyyy HH:mm:ss"); //cellDateTime.SetValue(DateTime.Now, "MM/dd/yyyy"); //cellDateTime.SetValue(DateTime.Now, "HH:mm"); // Save and show the spreedsheet if (File.Exists("sample.xlsx")) File.Delete("sample.xlsx"); workBook.Save("sample.xlsx"); Process.Start("sample.xlsx");
More Excel tutorial
- How to use XsExcel Component for .NET to change Excel Document cell data in C#.NET
- CSharp .NET online sample for inserting SQL database to MS Excel
- Spreedsheets converted from SQL database - Visual C# guide
- Excel Document numeric data formatted - .NET Visual C# guide
- C#.NET online demo for exporting dataset to Microsoft Excel