如何在Excel中将Oracle内部日期转换为Excel日期?

时间:2013-03-28 14:26:05

标签: oracle excel

我有一个从Oracle数据库中捕获的.csv文件。 .csv文件中的日期采用Oracle内部格式(不确定它的名称,因为我不熟悉Oracle)。它看起来像16474

当我在用于访问数据库的应用中查看该日期时,日期为2/6/2013

但是,当我将.csv文件导入Excel并将列格式化为短日期时,它会将日期显示为2/6/1945

我做了一个随机抽样,看起来月/日总是正确的,但是年份已经过了68年。我假设这是因为Oracle的“开始时间”与Excel的“开始时间”不同

有什么方法可以解决这个问题,以便在将Oracle数据导入Excel时正确显示日期?

我无法访问Oracle方面的任何内容..只有.csv文件和Excel。如果有帮助的话,我最终还是将Excel文件导入Access。

到目前为止,我将Oracle日期格式化为Excel中的短日期,并在我的日期列(J)旁边插入一列并使用以下内容:

=DATE(YEAR(J1)+68,MONTH(J1),DAY(J1))

考虑到闰年,我不确定这是否100%准确

编辑:我认为这段代码可能会这样做:

Public Sub ReplaceData()

    Dim RangeCell As Range

    For Each RangeCell In Selection

        If IsNumeric(RangeCell.Value) Then
            lngDate = CLng(RangeCell.Value)
            RangeCell.Value = DateAdd("d", lngDate - 1, "1/1/1968")
        End If

    Next RangeCell

End Sub

1 个答案:

答案 0 :(得分:2)

似乎Oracle可能会在1968年1月1日开始。我不是一个数据库人,但我认为这个数字在我所做的其他阅读中听起来很熟悉。

以下是将Oracle日期转换为Excel日期的示例例程,如果上述1/1/1968的假设为真。

Sub OracleToExcelCSVDates()
Dim rng as Range 'range of cells containing dates'
Dim cl as Range
Dim strDate As String 'date received from Oracle'
Dim lngDate As Long
Dim newDate As Date 'Converted date'

Set rng = Range("J1:J1000") '< modify this for the number of rows you use in col J'

For each cl in rng 'Iterate over the rng you defined above.'
    'Capture the date from Oracle'
    strDate = cl.Value
    If Not Trim(strDate) = vbNullString Then 'ignore blanks'
        'convert to Long data type'
        lngDate = CLng(strDate)

        'Add the # of days (lngDate) to Oracles base date of 1/1/1968'
        newDate = DateAdd("d", lngDate - 1, "1/1/1968")

        'Overwrite the cell value with the new converted date:'
        cl.Value = newDate
    End If

End Sub

有关Excel日期的更多信息:

Excel将序列日期存储为“表示自1900年1月1日以来的天数的数字,加上24小时工作日的小数部分:ddddd.tttttt。这称为序列日期或序列日期时间。 “

http://www.cpearson.com/excel/datetime