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");

Notice - If you used the trial version of XsExcel SDK, can only edit, insert or get the first 300 row data!

More Excel tutorial