Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 70 additions & 0 deletions knowledge-base/changing-date-format-exported-excel-blazor-grid.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: Changing Date Format of Exported Excel File from UI Grid components
description: Learn how to change the Date format in exported Excel files generated by data from a Grid component using Telerik Document Processing RadSpreadProcessing.
type: how-to
page_title: Formatting Date Columns in Exported Excel Files from Blazor Grid
meta_title: Formatting Date Columns in Exported Excel Files from Blazor Grid
slug: changing-date-format-exported-excel-blazor-grid
tags: spread,processing, telerik, document, grid, excel, date, format
res_type: kb
ticketid: 1702751
---

## Environment

| Version | Product | Author |
| ---- | ---- | ---- |
| 2025.3.806| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)|

## Description

Learn how to apply a custom date format (e.g., "yyyy-MM-dd") to the relevant columns in the exported Excel file generated from UI Grid components offered by the Telerik family.

<img style="border: 1px solid gray;" src="images/changing-date-format-exported-excel-blazor-grid.png" />

## Solution

To change the date format in the exported Excel file, use [RadSpreadProcessing]({%slug radspreadprocessing-overview%}) to modify the exported stream before saving the file.

Follow these steps:

1. Export the grid data to a `MemoryStream`.
2. Load the exported stream into a workbook using the [XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}).
3. Apply a custom [date format]({%slug radspreadprocessing-features-format-codes%}#date-and-time-formatting) to the desired column in the workbook.
4. Save the modified workbook to a file.

Here is an example implementation:

```csharp
static void Main(string[] args)
{
// Step 1: Export the grid data to a MemoryStream
var exportedExcelStream = new MemoryStream(File.ReadAllBytes("exported-grid.xlsx"));

// Step 2: Load the exported stream into a workbook
XlsxFormatProvider formatProvider = new XlsxFormatProvider();
Workbook workbook = formatProvider.Import(exportedExcelStream, TimeSpan.FromSeconds(10));

// Step 3: Apply date format to the desired column (e.g., column index 2 for Dates)
CellValueFormat dateFormat = new CellValueFormat("yyyy-MM-dd");
ColumnSelection dateColumn = workbook.Worksheets[0].Columns[2]; // Update index as needed
dateColumn.SetFormat(dateFormat);

// Step 4: Save the modified workbook to a file
string outputFilePath = "formatted.xlsx";
File.WriteAllBytes(outputFilePath, formatProvider.Export(workbook, TimeSpan.FromSeconds(10)));
Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true });
}
```

### Explanation

- **Step 1:** Reads the exported Excel file into a `MemoryStream`.
- **Step 2:** Utilizes `XlsxFormatProvider` to parse the stream into a `Workbook` object for manipulation.
- **Step 3:** Sets a custom date format for the targeted column using `SetFormat`.
- **Step 4:** Saves the updated workbook and opens the file using the default application.

## See Also

- [XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%})
- [Date and Time Formatting]({%slug radspreadprocessing-features-format-codes%}#date-and-time-formatting)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions libraries/radspreadprocessing/features/format-codes.md
Original file line number Diff line number Diff line change
Expand Up @@ -418,3 +418,4 @@ The regional currency settings determine the position of the currency symbol rel
## See Also

* [Number Formatting]({%slug radspreadprocessing-features-number-formats%})
* [Changing Date Format of Exported Excel File from UI Grid components]({%slug changing-date-format-exported-excel-blazor-grid%})