![](/img/trans.png)
[英]Converting a Datetime from the datatable to a specific format of string C# ASP.NET
[英]Date format changing while converting from excel to datatable in asp.net c#
我試圖導入excel,excel中的日期列正在更改為數字格式。 例如,我在excel Name,age和Dob中有3列
當我嘗試將此excel轉換為數據表時,Dob列中的日期將更改為數字。
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);
}
}
}
嘗試這個,
double val= double.Parse("42552");
DateTime requiredDate= DateTime.FromOADate(val);
請通過更改單元格的格式來嘗試這一點。
Range r = (Excel.Range)worksheetobject.Cells[row,column];
r.EntireColumn.NumberFormat = "MM/DD/YYYY";
並能做到這一點還,
double value = double.Parse(convertedNumber);
DateTime yourData = DateTime.FromOADate(value);
將列類型設置為“Text”並設置格式:
worksheet.Cells[row, col].NumberFormat = "@";
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.