Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date format changing while converting from excel to datatable in asp.net c#

Tags:

c#

asp.net

I have tried to import the excel the date column in excel is changing to number format. For example I have 3 column in the excel Name,age and Dob

When I try to convert this excel into the datatable the date in the Dob column is changing to a number.

using (ExcelPackage excelPackage = new ExcelPackage(fi))      
{
    ExcelWorksheet worksheet = excelPackage.Workbook.Worksheets[sheetName];
    ExcelCellAddress startCell = worksheet.Dimension.Start;
    ExcelCellAddress endCell = worksheet.Dimension.End;
    if (endCell.Row > 0)
    {
        for (int col = startCell.Column; col <= endCell.Column; col++)
        {
            dt.Columns.Add(Convert.ToString(worksheet.Cells[startCell.Row, col].Value));
        }

        for (int row = startCell.Row + 1; row <= endCell.Row; row++)
        {
            DataRow dr = dt.NewRow();
            int x = 0;
            for (int col = startCell.Column; col <= endCell.Column; col++)enter code here
            {
                dr[x++] = worksheet.Cells[row, col].Value;
            }

            dt.Rows.Add(dr);
        }
    }
}
like image 353
keerthi Avatar asked Jan 18 '26 13:01

keerthi


1 Answers

Try This,

double val= double.Parse("42552");
DateTime requiredDate= DateTime.FromOADate(val);
like image 154
Nimmi Avatar answered Jan 20 '26 05:01

Nimmi