Skip to content

Commit

Permalink
Add custom column format for Excel export (gustavnavar/Grid.Blazor#376)
Browse files Browse the repository at this point in the history
  • Loading branch information
dev0926 committed Mar 4, 2023
1 parent d8c7996 commit fb738fd
Show file tree
Hide file tree
Showing 12 changed files with 80 additions and 1 deletion.
11 changes: 11 additions & 0 deletions GridBlazor/Columns/GridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,17 @@ internal override IGridColumn<T> 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);
Expand Down
8 changes: 8 additions & 0 deletions GridBlazor/Columns/GridColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public abstract class GridColumnBase<T> : GridStyledColumn, IGridColumn<T>, IExp
public bool EnableCard { get; private set; } = true;

public Func<T, string> ValueConstraint { get; private set; }
public Func<T, string> ExcelConstraint { get; private set; }
public string ValuePattern { get; private set; }

#region IGridColumn<T> Members
Expand Down Expand Up @@ -224,6 +225,12 @@ public IGridColumn<T> RenderValueAs(Func<T, string> constraint)
return this;
}

public IGridColumn<T> RenderExcelAs(Func<T, string> constraint)
{
ExcelConstraint = constraint;
return this;
}

public IGridColumn<T> RenderComponentAs(Type componentType)
{
return RenderComponentAs(componentType, null, null);
Expand Down Expand Up @@ -732,6 +739,7 @@ public IGridColumn<T> SetInitialFilter(GridFilterType type, string value)
public abstract IGridColumn<T> Sortable(bool sort,GridSortMode gridSortMode = GridSortMode.ThreeState);
internal abstract IGridColumn<T> InternalSortable(bool sort, GridSortMode gridSortMode = GridSortMode.ThreeState);

public abstract IGridCell GetExcelCell(object instance);
public abstract IGridCell GetCell(object instance);

public string GetFormatedValue(object value)
Expand Down
2 changes: 1 addition & 1 deletion GridBlazor/ExcelWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public byte[] GenerateExcel<T>(IGridColumnCollection<T> columns, IEnumerable<T>
{
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<T>)column).GetTypeAndValue(item).Type;
row.Add(new ExcelCell(cell.ToString(), type));
Expand Down
5 changes: 5 additions & 0 deletions GridCore/Columns/GridCoreColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 6 additions & 0 deletions GridCore/Columns/GridCoreColumnBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,11 @@ public IGridColumn<T> RenderValueAs(Func<T, string> constraint)
return this;
}

public IGridColumn<T> RenderExcelAs(Func<T, string> constraint)
{
return this;
}

public IGridColumn<T> RenderComponentAs(Type componentType)
{
return RenderComponentAs(componentType, null, null);
Expand Down Expand Up @@ -729,6 +734,7 @@ public IGridColumn<T> SetInitialFilter(GridFilterType type, string value)


public abstract IGridCell GetCell(object instance);
public abstract IGridCell GetExcelCell(object instance);

public string GetFormatedValue(object value)
{
Expand Down
7 changes: 7 additions & 0 deletions GridShared/Columns/IGridColumn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,11 @@ public interface IColumn<T>
/// </summary>
IGridColumn<T> RenderValueAs(Func<T, string> constraint);

/// <summary>
/// Setup the custom rendere for property
/// </summary>
IGridColumn<T> RenderExcelAs(Func<T, string> constraint);

/// <summary>
/// Setup the custom render for component
/// </summary>
Expand Down Expand Up @@ -549,6 +554,8 @@ public interface IColumn
/// <param name="instance">Instance of the item</param>
IGridCell GetCell(object instance);

IGridCell GetExcelCell(object instance);

/// <summary>
/// Gets gridColumn formated value
/// </summary>
Expand Down
7 changes: 7 additions & 0 deletions docs/blazor_client/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions docs/blazor_grpc/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions docs/blazor_local/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions docs/blazor_odata/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions docs/blazor_server/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions docs/dotnetcore_blazor/Excel_export.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit fb738fd

Please sign in to comment.