From 5f84c17e4fe9ffda0430d946575f17adcf9667c0 Mon Sep 17 00:00:00 2001 From: StenlyGrigorov Date: Fri, 8 Nov 2024 15:32:31 +0200 Subject: [PATCH] Update export-datetime.md --- .../export/how-to/export-datetime.md | 64 ++++++++++++++----- 1 file changed, 49 insertions(+), 15 deletions(-) diff --git a/controls/radgridview/export/how-to/export-datetime.md b/controls/radgridview/export/how-to/export-datetime.md index 5b6637c123..8d86a59bc6 100644 --- a/controls/radgridview/export/how-to/export-datetime.md +++ b/controls/radgridview/export/how-to/export-datetime.md @@ -1,7 +1,7 @@ --- title: Export DateTime Value page_title: Export DateTime Value -description: Read this article to learn how to export DateTime values from Telerik's {{ site.framework_name }} DataGrid without having the values appear as numbers. +description: Read this article to learn how to export DateTime values from Telerik's DataGrid without having the values appear as numbers. slug: gridview-export-datetime tags: export, datetime, value published: True @@ -12,17 +12,17 @@ position: 5 __PROBLEM__ -1) When exporting DateTime values without a specified **DataFormatString** to Excel, the values appear as numbers as shown in **Figure 1**: +1) When exporting DateTime values without a specified __DataFormatString__ to Excel, the values appear as numbers as shown in the below image: -#### __Figure 1: Exporting DateTime values without specified DataFormatString__ +__Exporting DateTime values without specified DataFormatString__ -![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-number](images/gridview-export-datetime-as-number.png) +![Telerik DataGrid-export-datetime-as-number](images/gridview-export-datetime-as-number.png) -2) When exporting DateTime values with specified **DataFormatString** to Excel, the values appear as strings as shown in **Figure 2**: +2) When exporting DateTime values with specified __DataFormatString__ to Excel, the values appear as strings as shown in the next image: -#### __Figure 2: Exporting DateTime values with specified DataFormatString__ +__Exporting DateTime values with specified DataFormatString__ -![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-string](images/gridview-export-datetime-as-string.png) +![Telerik DataGrid-export-datetime-as-string](images/gridview-export-datetime-as-string.png) __CAUSE__ @@ -30,12 +30,13 @@ __CAUSE__ 2) When a DataFormatString has been specified for a given column, RadGridView exports the string representation of the values in that column. -__SOLUTION__ +__SOLUTIONs__ -When an element is exported through the [ExportToXlsx]({%slug gridview-export-xlsx%}), [ExportToPdf]({%slug gridview-export-pdf%}), [ExportToWorkbook]({%slug gridview-export-workbook%}) or [SpreadsheetStreamingExport]({%slug gridview-export-spreadsheetstreamingexport%}) methods, the arguments of the [ElementExportingToDocument]({%slug gridview-export-events-elementexporting-elementexported-todocument%}) event can be used to modify the visual appearance of the exported values and specify how they should be [formatted](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/format-codes#date-and-time-formatting) in Excel. This is achieved through the **VisualParameters** property of the **GridViewCellExportingEventArgs**. +## Export via the Built-In RadGridView Export Methods -#### __[C#] Example 1: Exporting DateTime Values to Excel__ +When an element is exported through the [ExportToXlsx]({%slug gridview-export-xlsx%}), [ExportToPdf]({%slug gridview-export-pdf%}) or [ExportToWorkbook]({%slug gridview-export-workbook%}) or methods, the arguments of the [ElementExportingToDocument]({%slug gridview-export-events-elementexporting-elementexported-todocument%}) event can be used to modify the visual appearance of the exported values and specify how they should be [formatted](https://docs.telerik.com/devtools/document-processing/libraries/radspreadprocessing/features/format-codes#date-and-time-formatting) in Excel. This is achieved through the `VisualParameters` property of the `GridViewCellExportingEventArgs`. +#### __[C#] Exporting DateTime Values to Excel__ {{region cs-gridview-export-how-to-export-datetime-1}} this.radGridView.ElementExportingToDocument += (s, e) => { @@ -54,8 +55,7 @@ When an element is exported through the [ExportToXlsx]({%slug gridview-export-xl }; {{endregion}} -#### __[VB.NET] Example 1: Exporting DateTime Values to Excel__ - +#### __[VB.NET] Exporting DateTime Values to Excel__ {{region vb-gridview-export-how-to-export-datetime-2}} AddHandler Me.radGridView.ElementExportingToDocument, Sub(s, e) If e.Element = ExportElement.Cell Then @@ -65,12 +65,46 @@ When an element is exported through the [ExportToXlsx]({%slug gridview-export-xl parameters.Style = New CellSelectionStyle() With {.Format = New CellValueFormat("m/d/yyyy")} End If End If - End Sub + End Sub {{endregion}} -#### __Figure 3: Exporting DateTime values with ElementExportingToDocument__ +__Exporting DateTime values with ElementExportingToDocument__ + +![Telerik DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png) + +## Export via the GridViewSpreadStreamExport Class -![Telerik {{ site.framework_name }} DataGrid-export-datetime-as-datetime](images/gridview-export-datetime-as-datetime.png) +When exporting the RadGridView with the [GridViewSpreadStreamExport class]({%slug gridview-export-spreadsheetstreamingexport%}), the event arguments of `ElementExportingToDocument` event will be of the type of `GridViewSpreadStreamElementExportingEventArgs`. To format the number value, create a new `SpreadCellFormat` instance and set the `NumberFormat` property. To apply the formatting, create a new SpreadStreamCellStyle instance, set the created `SpreadCellFormat` to its `CellFormat` property, and apply it to the `e.Style` property of the event arguments. + +#### __[C#] Specify a format when exporting with the GridViewSpreadStreamExport class__ +{{region gridview-export-how-to-export-datetime-3}} + private static void SpreadStreamExport_ElementExportingToDocument(object sender, GridViewSpreadStreamElementExportingEventArgs e) + { + if (e.Element == SpreadStreamExportElement.Cell && e.Value is DateTime) + { + e.Style = new SpreadStreamCellStyle() + { + CellFormat = new SpreadCellFormat() + { + NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() + " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM() + } + }; + } + } +{{endregion}} + +#### __[VB.NET] Specify a format when exporting with the GridViewSpreadStreamExport class__ +{{region gridview-export-how-to-export-datetime-4}} + Private Shared Sub SpreadStreamExport_ElementExportingToDocument(ByVal sender As Object, ByVal e As GridViewSpreadStreamElementExportingEventArgs) + If e.Element = SpreadStreamExportElement.Cell AndAlso TypeOf e.Value Is DateTime Then + e.Style = New SpreadStreamCellStyle() With { + .CellFormat = New SpreadCellFormat() With { + .NumberFormat = BuiltInNumberFormats.GetDayMonthLongYear() & " " + BuiltInNumberFormats.GetHourMinuteSecondAMPM() + } + } + End If + End Sub +{{endregion}} ## See Also