excel中的单元格日期格式显示日期时间

时间:2016-05-20 03:08:25

标签: vb.net excel date datetime datagridview

我有一个应用程序可以检索安装日期并将其保存在datagridview列中,格式仅为日期。

但是当我尝试将其导出到excel文件时,即使我尝试格式化单元格,它仍然只显示某些数据的日期时间而不是日期。

This is how the datagridview looks like

This is how the excel file looks like

导出代码如下所示。顺便说一句,我正在使用vb.net

'reportFile : True = IE_Version_Report.xlsx      False = Data.xlsx
Sub ExportData(reportFile)
    Dim dSet As New DataSet
    dSet.Tables.Add()
    For i As Integer = 0 To DataGridView1.ColumnCount - 1
        dSet.Tables(0).Columns.Add(DataGridView1.Columns(i).HeaderText)
    Next

    Dim dr As DataRow

    For i As Integer = 0 To DataGridView1.RowCount - 1
        dr = dSet.Tables(0).NewRow
        For j As Integer = 0 To DataGridView1.Columns.Count - 1
            dr(j) = DataGridView1.Rows(i).Cells(j).Value
        Next
        dSet.Tables(0).Rows.Add(dr)
    Next

    Dim Ex As Microsoft.Office.Interop.Excel.Application
    Dim Wb As Microsoft.Office.Interop.Excel.Workbook
    Dim Ws As Microsoft.Office.Interop.Excel.Worksheet
    Dim misValue As Object = System.Reflection.Missing.Value

    Ex = New Microsoft.Office.Interop.Excel.Application
    Wb = Ex.Workbooks.Add(misValue)
    Ws = Wb.Sheets("sheet1")

    Dim dt As DataTable = dSet.Tables(0)
    Dim col, row As Integer
    Dim rawData(dt.Rows.Count, dt.Columns.Count - 1) As Object

    For col = 0 To dt.Columns.Count - 1
        rawData(0, col) = dt.Columns(col).ColumnName.ToUpper
    Next

    For col = 0 To dt.Columns.Count - 1
        For row = 0 To dt.Rows.Count - 1
            rawData(row + 1, col) = dt.Rows(row).ItemArray(col)
        Next
    Next
    Dim finalColLetter As String = String.Empty
    finalColLetter = ExcelColName(dt.Columns.Count)

    Dim excelRange As String = String.Format("A1:{0}{1}", finalColLetter, dt.Rows.Count + 1)

    Ws.Range(excelRange, Type.Missing).Value2 = rawData
    Ws.Range("A1:L1").EntireColumn.AutoFit() 'columns in excel file will autofit according to the data

    Dim num As Integer = DataGridView1.Rows.Count + 1

    'set the format for dates in these few columns
    Ws.Range("D2:D" & num).NumberFormat = "dd/mm/yyyy;@"
    Ws.Range("F2:F" & num).NumberFormat = "dd/mm/yyyy;@"
    Ws.Range("H2:H" & num).NumberFormat = "dd/mm/yyyy;@"
    Ws.Range("J2:J" & num).NumberFormat = "dd/mm/yyyy;@"
    Ws.Range("L2:L" & num).NumberFormat = "dd/mm/yyyy;@"


    Ws = Nothing

    If reportFile = True Then
        fileExported = True
        If System.IO.File.Exists(FolderPath & "\MSOffice_Report.xlsx") Then
            System.IO.File.Delete(FolderPath & "\MSOffice_Report.xlsx")
        End If
        Wb.SaveAs(FolderPath & "\MSOffice_Report.xlsx")
    Else
        fileExported = False
        If System.IO.File.Exists("C:\Install\MSData.xlsx") Then
            System.IO.File.Delete("C:\Install\MSData.xlsx")
        End If
        Wb.SaveAs("C:\Install\MSData.xlsx")
        SetAttr("C:\Install\MSData.xlsx", vbHidden)
    End If

    Wb.Close(True)
    Wb = Nothing
    Ex.Quit()
    Ex = Nothing
    GC.Collect()

End Sub

Public Function ExcelColName(ByVal Col As Integer) As String
    If Col < 0 And Col > 256 Then
        MsgBox("Invalid Argument", MsgBoxStyle.Critical)
        Return Nothing
        Exit Function
    End If
    Dim i As Int16
    Dim r As Int16
    Dim S As String
    If Col <= 26 Then
        S = Chr(Col + 64)
    Else
        r = Col Mod 26
        i = System.Math.Floor(Col / 26)
        If r = 0 Then
            r = 26
            i = i - 1
        End If
        S = Chr(i + 64) & Chr(r + 64)
    End If
    ExcelColName = S
End Function

很抱歉,如果这是一个非常愚蠢的问题,但我真的不知道它有什么问题。

谢谢!

1 个答案:

答案 0 :(得分:0)

Excel无法识别早于1/1/1900的日期,并会将11/11/1111 12:00:00 AM之类的值视为文本。查看日期是如何右对齐的,文字在屏幕截图中保持对齐状态?

文本不会受到您应用的数字格式的影响,只显示值的日期。

根据您要实现的目标,您需要调整代码以不同方式处理文本值。用1/1/1900或其他一些替换它们。