|
| 1 | +--- |
| 2 | +title: Changing Date Format of Exported Excel File from UI Grid components |
| 3 | +description: Learn how to change the Date format in exported Excel files generated by data from a Grid component using Telerik Document Processing RadSpreadProcessing. |
| 4 | +type: how-to |
| 5 | +page_title: Formatting Date Columns in Exported Excel Files from Blazor Grid |
| 6 | +meta_title: Formatting Date Columns in Exported Excel Files from Blazor Grid |
| 7 | +slug: changing-date-format-exported-excel-blazor-grid |
| 8 | +tags: spread,processing, telerik, document, grid, excel, date, format |
| 9 | +res_type: kb |
| 10 | +ticketid: 1702751 |
| 11 | +--- |
| 12 | + |
| 13 | +## Environment |
| 14 | + |
| 15 | +| Version | Product | Author | |
| 16 | +| ---- | ---- | ---- | |
| 17 | +| 2025.3.806| RadSpreadProcessing |[Desislava Yordanova](https://www.telerik.com/blogs/author/desislava-yordanova)| |
| 18 | + |
| 19 | +## Description |
| 20 | + |
| 21 | +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. |
| 22 | + |
| 23 | +<img style="border: 1px solid gray;" src="images/changing-date-format-exported-excel-blazor-grid.png" /> |
| 24 | + |
| 25 | +## Solution |
| 26 | + |
| 27 | +To change the date format in the exported Excel file, use [RadSpreadProcessing]({%slug radspreadprocessing-overview%}) to modify the exported stream before saving the file. |
| 28 | + |
| 29 | +Follow these steps: |
| 30 | + |
| 31 | +1. Export the grid data to a `MemoryStream`. |
| 32 | +2. Load the exported stream into a workbook using the [XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}). |
| 33 | +3. Apply a custom [date format]({%slug radspreadprocessing-features-format-codes%}#date-and-time-formatting) to the desired column in the workbook. |
| 34 | +4. Save the modified workbook to a file. |
| 35 | + |
| 36 | +Here is an example implementation: |
| 37 | + |
| 38 | +```csharp |
| 39 | +static void Main(string[] args) |
| 40 | +{ |
| 41 | + // Step 1: Export the grid data to a MemoryStream |
| 42 | + var exportedExcelStream = new MemoryStream(File.ReadAllBytes("exported-grid.xlsx")); |
| 43 | + |
| 44 | + // Step 2: Load the exported stream into a workbook |
| 45 | + XlsxFormatProvider formatProvider = new XlsxFormatProvider(); |
| 46 | + Workbook workbook = formatProvider.Import(exportedExcelStream, TimeSpan.FromSeconds(10)); |
| 47 | + |
| 48 | + // Step 3: Apply date format to the desired column (e.g., column index 2 for Dates) |
| 49 | + CellValueFormat dateFormat = new CellValueFormat("yyyy-MM-dd"); |
| 50 | + ColumnSelection dateColumn = workbook.Worksheets[0].Columns[2]; // Update index as needed |
| 51 | + dateColumn.SetFormat(dateFormat); |
| 52 | + |
| 53 | + // Step 4: Save the modified workbook to a file |
| 54 | + string outputFilePath = "formatted.xlsx"; |
| 55 | + File.WriteAllBytes(outputFilePath, formatProvider.Export(workbook, TimeSpan.FromSeconds(10))); |
| 56 | + Process.Start(new ProcessStartInfo() { FileName = outputFilePath, UseShellExecute = true }); |
| 57 | +} |
| 58 | +``` |
| 59 | + |
| 60 | +### Explanation |
| 61 | + |
| 62 | +- **Step 1:** Reads the exported Excel file into a `MemoryStream`. |
| 63 | +- **Step 2:** Utilizes `XlsxFormatProvider` to parse the stream into a `Workbook` object for manipulation. |
| 64 | +- **Step 3:** Sets a custom date format for the targeted column using `SetFormat`. |
| 65 | +- **Step 4:** Saves the updated workbook and opens the file using the default application. |
| 66 | + |
| 67 | +## See Also |
| 68 | + |
| 69 | +- [XlsxFormatProvider]({%slug radspreadprocessing-formats-and-conversion-xlsx-xlsxformatprovider%}) |
| 70 | +- [Date and Time Formatting]({%slug radspreadprocessing-features-format-codes%}#date-and-time-formatting) |
0 commit comments