[英]How to show date field of data table in exported Excel in 'dd MMM yyyy' format
我可以在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?
思路:
-VBA: Columns("E:E").NumberFormat = "dd MMM yyyy"
-Excel Formula: =TEXT(TODAY(),"dd MMM yyyy")
和VBA:
Columns("E:E").Copy Columns("E:E").PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _ xlNone, SkipBlanks:=False, Transpose:=False
- 將日期單元格導出為文本,如果第一個字符是'
撇號,則Excel始終將單元格作為文本讀取。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.