diff --git a/knowledge-base/changing-date-format-exported-excel-blazor-grid.md b/knowledge-base/changing-date-format-exported-excel-blazor-grid.md new file mode 100644 index 00000000..fb283bfe --- /dev/null +++ b/knowledge-base/changing-date-format-exported-excel-blazor-grid.md @@ -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. + + + +## 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) diff --git a/knowledge-base/images/changing-date-format-exported-excel-blazor-grid.png b/knowledge-base/images/changing-date-format-exported-excel-blazor-grid.png new file mode 100644 index 00000000..4114e794 Binary files /dev/null and b/knowledge-base/images/changing-date-format-exported-excel-blazor-grid.png differ diff --git a/libraries/radspreadprocessing/features/format-codes.md b/libraries/radspreadprocessing/features/format-codes.md index 2ffdc03f..b8775e17 100644 --- a/libraries/radspreadprocessing/features/format-codes.md +++ b/libraries/radspreadprocessing/features/format-codes.md @@ -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%})