2015-11-02 28 views
3

我能夠在GridView中顯示日期字段值dd MMM yyyy(2015年10月29日)。導出到Excel後,日期字段值格式將更改爲:dd-MMM-yy(29-Oct-15)。我的代碼:如何以'dd MMM yyyy'格式在導出的Excel中顯示數據表的日期字段

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?

回答

0

思路:

-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

-Export日期單元爲文本,Excel中總是讀一個單元格爲文本如果第一個字符是撇號'

相關問題