diff --git a/GridBlazor/Columns/GridColumn.cs b/GridBlazor/Columns/GridColumn.cs index edcacabd..28823bd2 100644 --- a/GridBlazor/Columns/GridColumn.cs +++ b/GridBlazor/Columns/GridColumn.cs @@ -264,6 +264,17 @@ internal override IGridColumn InternalSortable(bool sort, GridSortMode gridSo return this; } + public override IGridCell GetExcelCell(object instance) + { + if (ExcelConstraint != null) + { + var textValue = ExcelConstraint((T)instance); + return new GridCell(textValue) { Encode = EncodeEnabled }; + } + else + return GetValue((T)instance); + } + public override IGridCell GetCell(object instance) { return GetValue((T)instance); diff --git a/GridBlazor/Columns/GridColumnBase.cs b/GridBlazor/Columns/GridColumnBase.cs index 016e192d..eb2acf49 100644 --- a/GridBlazor/Columns/GridColumnBase.cs +++ b/GridBlazor/Columns/GridColumnBase.cs @@ -37,6 +37,7 @@ public abstract class GridColumnBase : GridStyledColumn, IGridColumn, IExp public bool EnableCard { get; private set; } = true; public Func ValueConstraint { get; private set; } + public Func ExcelConstraint { get; private set; } public string ValuePattern { get; private set; } #region IGridColumn Members @@ -224,6 +225,12 @@ public IGridColumn RenderValueAs(Func constraint) return this; } + public IGridColumn RenderExcelAs(Func constraint) + { + ExcelConstraint = constraint; + return this; + } + public IGridColumn RenderComponentAs(Type componentType) { return RenderComponentAs(componentType, null, null); @@ -732,6 +739,7 @@ public IGridColumn SetInitialFilter(GridFilterType type, string value) public abstract IGridColumn Sortable(bool sort,GridSortMode gridSortMode = GridSortMode.ThreeState); internal abstract IGridColumn InternalSortable(bool sort, GridSortMode gridSortMode = GridSortMode.ThreeState); + public abstract IGridCell GetExcelCell(object instance); public abstract IGridCell GetCell(object instance); public string GetFormatedValue(object value) diff --git a/GridBlazor/ExcelWriter.cs b/GridBlazor/ExcelWriter.cs index ffca1961..70ded4a1 100644 --- a/GridBlazor/ExcelWriter.cs +++ b/GridBlazor/ExcelWriter.cs @@ -157,7 +157,7 @@ public byte[] GenerateExcel(IGridColumnCollection columns, IEnumerable { if (!(column.ExcelHidden ?? column.Hidden)) { - var cell = column.GetCell(item) as GridCell; + var cell = column.GetExcelCell(item) as GridCell; cell.Encode = false; var type = ((IGridColumn)column).GetTypeAndValue(item).Type; row.Add(new ExcelCell(cell.ToString(), type)); diff --git a/GridCore/Columns/GridCoreColumn.cs b/GridCore/Columns/GridCoreColumn.cs index de21628c..5bb29cda 100644 --- a/GridCore/Columns/GridCoreColumn.cs +++ b/GridCore/Columns/GridCoreColumn.cs @@ -246,6 +246,11 @@ public override IGridCell GetCell(object instance) return GetValue((T)instance); } + public override IGridCell GetExcelCell(object instance) + { + return GetValue((T)instance); + } + public override IGridCell GetValue(T instance) { string textValue; diff --git a/GridCore/Columns/GridCoreColumnBase.cs b/GridCore/Columns/GridCoreColumnBase.cs index ee024703..957d310b 100644 --- a/GridCore/Columns/GridCoreColumnBase.cs +++ b/GridCore/Columns/GridCoreColumnBase.cs @@ -219,6 +219,11 @@ public IGridColumn RenderValueAs(Func constraint) return this; } + public IGridColumn RenderExcelAs(Func constraint) + { + return this; + } + public IGridColumn RenderComponentAs(Type componentType) { return RenderComponentAs(componentType, null, null); @@ -729,6 +734,7 @@ public IGridColumn SetInitialFilter(GridFilterType type, string value) public abstract IGridCell GetCell(object instance); + public abstract IGridCell GetExcelCell(object instance); public string GetFormatedValue(object value) { diff --git a/GridShared/Columns/IGridColumn.cs b/GridShared/Columns/IGridColumn.cs index 64f0aeec..d2b8596d 100644 --- a/GridShared/Columns/IGridColumn.cs +++ b/GridShared/Columns/IGridColumn.cs @@ -143,6 +143,11 @@ public interface IColumn /// IGridColumn RenderValueAs(Func constraint); + /// + /// Setup the custom rendere for property + /// + IGridColumn RenderExcelAs(Func constraint); + /// /// Setup the custom render for component /// @@ -549,6 +554,8 @@ public interface IColumn /// Instance of the item IGridCell GetCell(object instance); + IGridCell GetExcelCell(object instance); + /// /// Gets gridColumn formated value /// diff --git a/docs/blazor_client/Excel_export.md b/docs/blazor_client/Excel_export.md index c854c173..f1f9f7de 100644 --- a/docs/blazor_client/Excel_export.md +++ b/docs/blazor_client/Excel_export.md @@ -19,6 +19,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png) diff --git a/docs/blazor_grpc/Excel_export.md b/docs/blazor_grpc/Excel_export.md index 207f941d..4495fe5d 100644 --- a/docs/blazor_grpc/Excel_export.md +++ b/docs/blazor_grpc/Excel_export.md @@ -19,6 +19,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png) diff --git a/docs/blazor_local/Excel_export.md b/docs/blazor_local/Excel_export.md index a265581c..4c21c17c 100644 --- a/docs/blazor_local/Excel_export.md +++ b/docs/blazor_local/Excel_export.md @@ -19,6 +19,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png) diff --git a/docs/blazor_odata/Excel_export.md b/docs/blazor_odata/Excel_export.md index 8ada9f3c..5930143e 100644 --- a/docs/blazor_odata/Excel_export.md +++ b/docs/blazor_odata/Excel_export.md @@ -19,6 +19,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png) diff --git a/docs/blazor_server/Excel_export.md b/docs/blazor_server/Excel_export.md index c3e91a09..470da65d 100644 --- a/docs/blazor_server/Excel_export.md +++ b/docs/blazor_server/Excel_export.md @@ -19,6 +19,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png) diff --git a/docs/dotnetcore_blazor/Excel_export.md b/docs/dotnetcore_blazor/Excel_export.md index 3b2f41d2..a0ebb9f1 100644 --- a/docs/dotnetcore_blazor/Excel_export.md +++ b/docs/dotnetcore_blazor/Excel_export.md @@ -20,6 +20,13 @@ enabled | bool to enable Excel export (required) allRows | bool to enable export of all grid rows (optional). The default value is false and only visible rows are exported fileName | string to define the file name (optional). If no value is defined, the grid internal name is used as file name +You can construct a custom display value of the column only for the exported file using the ```RenderExcelAs``` method: + +```c# + Columns.Add(o => o.Employees.LastName) + .RenderExcelAs(o => o.Employees.FirstName + " " + o.Employees.LastName) +``` + This is an example of a grid with an export to Excel button: ![](../images/Excel.png)