Skip to content

Commit

Permalink
Merge pull request #2976 from telerik/stennlyy/d-SpreadsheetStreaming…
Browse files Browse the repository at this point in the history
…Export-export-DateTime

GridView: Update Export DateTime Value article with example for SpreadsheetStreamingExport
  • Loading branch information
martinivanoff authored Nov 18, 2024
2 parents 21dfca3 + 5f84c17 commit 827896e
Showing 1 changed file with 49 additions and 15 deletions.
64 changes: 49 additions & 15 deletions controls/radgridview/export/how-to/export-datetime.md
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -12,30 +12,31 @@ 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__

1) In most modern programming environments, dates are stored as real numbers. The integer part of the number is the number of days since some agreed-upon date in the past, called the epoch. In Excel, June 16, 2006, for example, is stored as 38884, counting days where January 1st, 1900 is 1.

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) =>
{
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit 827896e

Please sign in to comment.