【问题标题】:How to show date field of data table in exported Excel in 'dd MMM yyyy' format如何以“dd MMM yyyy”格式在导出的 Excel 中显示数据表的日期字段
【发布时间】:2016-02-01 23:30:40
【问题描述】:

我能够在 dd MMM yyyy(2015 年 10 月 29 日) 的 GridView 中显示日期字段值。导出到 Excel 后,日期字段值格式变为:dd-MMM-yy (29-Oct-15)。我的代码:

Protected Sub btnExportData_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles btnExportData.ServerClick
    Try
        Dim str As String
        str = HiddenFilterString.Value
        Dim dtExportData As New DataTable
        Dim dtTemp As New DataTable
        Dim dg As New DataGrid
        dtTemp = DirectCast(Session("dtSoHoManageSecretServerFoldersExportData"), DataTable)

        dtExportData = dtTemp.Copy()
        Dim dv As New DataView(dtExportData)
        dg.DataSource = dv
        dg.DataBind()

        Dim sFileName As String = "ServerFolders.xls"
        HttpContext.Current.Response.Clear()
        HttpContext.Current.Response.ClearHeaders()
        HttpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.Buffer = True
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & sFileName)
        HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"
        Me.EnableViewState = False
        Dim objSW As New System.IO.StringWriter
        Dim objHTW As New HtmlTextWriter(objSW)
        dg.HeaderStyle.Font.Bold = True
        dg.RenderControl(objHTW)
        HttpContext.Current.Response.Write(objSW.ToString())
        HttpContext.Current.Response.Flush()
        HttpContext.Current.Response.End()

        dtExportData = Nothing
        dtTemp = Nothing
        dg = Nothing
        dv = Nothing
        objSW = Nothing
        objHTW = Nothing
    Catch ex As Exception
        ReportInternalError("btnExportData_Click", ex)
        Throw
    End Try
End Sub

是否可以在导出的 Excel 中保留相同格式的 GridView?

【问题讨论】:

    标签: asp.net excel date gridview


    【解决方案1】:

    想法:

    -VBA:Columns("E:E").NumberFormat = "dd MMM yyyy"

    -Excel 公式:=TEXT(TODAY(),"dd MMM yyyy") 与 VBA:

    Columns("E:E").Copy Columns("E:E").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False

    -将日期单元格导出为文本,如果第一个字符是' 撇号,Excel 总是将单元格读取为文本。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-30
      • 2018-02-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多