簡體   English   中英

從 Excel 工作表中讀取日期時間值

[英]Reading Datetime value From Excel sheet

當我試圖從 Excel 工作表中讀取日期時間類型值時,它返回一個雙精度值。例如,如果想像這樣讀取值'2007-02-19 14:11:45.730' ,我得到一個雙精度類型值。進一步我我正在使用時間跨度轉換這個雙精度值,但沒有成功完成,因為我只得到這個值'2007-02-19 12:00:00 AM'
現在我想要與第一個完全相同的日期時間值。 我的代碼是這樣的:-

TimeSpan datefromexcel = new TimeSpan(Convert.ToInt32((range.Cells[rCnt, cCnt] as Excel.Range).Value2), 0, 0, 0);

  DateTime inputdate = new DateTime(1900, 1, 1).Add(datefromexcel);

   arrrow2[cCnt - 1] = inputdate.ToString();

請幫忙。!! 謝謝。

您需要使用 DateTime.FromOADate 將日期格式從 OLE Automation 轉換為 .net 格式。

double d = double.Parse(b);
DateTime conv = DateTime.FromOADate(d);

也許您可以嘗試使用DateTime.FromOADate方法在 Excel 和 .net 之間進行轉換。

從 Excel 工作表中讀取日期時間值:試試這個就可以了。

string sDate = (xlRange.Cells[4, 3] as Excel.Range).Value2.ToString();

double date = double.Parse(sDate);

var dateTime = DateTime.FromOADate(date).ToString("MMMM dd, yyyy");

或者,如果您的單元格已經是真實日期,只需使用 .Value 而不是 .Value2:

excelApp.Range[namedRange].Value
{21/02/2013 00:00:00}
    Date: {21/02/2013 00:00:00}
    Day: 21
    DayOfWeek: Thursday
    DayOfYear: 52
    Hour: 0
    Kind: Unspecified
    Millisecond: 0
    Minute: 0
    Month: 2
    Second: 0
    Ticks: 634970016000000000
    TimeOfDay: {00:00:00}
    Year: 2013

excelApp.Range[namedRange].Value2
41326.0

我有類似的情況,我使用下面的代碼來完成這個工作..

Aspose.Cells.LoadOptions loadOptions = new Aspose.Cells.LoadOptions(Aspose.Cells.LoadFormat.CSV);

Workbook workbook = new Workbook(fstream, loadOptions);

Worksheet worksheet = workbook.Worksheets[0];

dt = worksheet.Cells.ExportDataTable(0, 0, worksheet.Cells.MaxDisplayRange.RowCount, worksheet.Cells.MaxDisplayRange.ColumnCount, true);

DataTable dtCloned = dt.Clone();
ArrayList myAL = new ArrayList();

foreach (DataColumn column in dtCloned.Columns)
{
    if (column.DataType == Type.GetType("System.DateTime"))
    {
        column.DataType = typeof(String);
        myAL.Add(column.ColumnName);
    }
}


foreach (DataRow row in dt.Rows)
{
    dtCloned.ImportRow(row);
}



foreach (string colName in myAL)
{
    dtCloned.Columns[colName].Convert(val => DateTime.Parse(Convert.ToString(val)).ToString("MMMM dd, yyyy"));
}


/*******************************/

public static class MyExtension
{
    public static void Convert<T>(this DataColumn column, Func<object, T> conversion)
    {
        foreach (DataRow row in column.Table.Rows)
        {
            row[column] = conversion(row[column]);
        }
    }
}

希望這對某些人有所幫助 thx_joxin

您可能想嘗試我在另一個與從 Excel 工作表讀取日期值相關的線程上發布的簡單函數。

它只是將單元格中的文本作為輸入,並將 DateTime 作為輸出。

我很高興看到為了 .Net 開發社區的利益而提供的示例代碼有所改進。

這是線程C# not reading excel date from spreadsheet的鏈接

或者您可以簡單地使用 OleDbDataAdapter 從 Excel 中獲取數據

另一種選擇:當單元格類型在編譯時未知並且單元格被格式化為 Date Range.Value時返回所需的DateTime對象。


public static DateTime? GetAsDateTimeOrDefault(Range cell)
{
    object cellValue = cell.Value;
    if (cellValue is DateTime result)
    {
        return result;
    }
    return null;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM