|
<EjsGrid ID="DeviceGrid" @ref="DeviceGrid" DataSource="Devices" Width="100%" Height="100%" ShowColumnChooser="true" AllowExcelExport="true" Toolbar=@ToolbarItems>
<GridEvents OnToolbarClick="ToolbarClick" TValue="FlattenDeviceModel"></GridEvents>
<GridColumns>
<GridColumn Field="Id" HeaderText="Device ID" Width="200" ShowInColumnChooser="false" />
<GridColumn Field="Name" HeaderText="Device Name" Width="150" ShowInColumnChooser="false" />
<GridColumn Field="Ledcolor" HeaderText="led_color" Width="150" ShowInColumnChooser="false" />
<GridColumn Field="Powerstate" HeaderText="power_state" Width="150" ShowInColumnChooser="false" />
<GridColumn Field="Powerofftime" HeaderText="power_off_time" Width="150" ShowInColumnChooser="false" />
...
</GridColumns>
</EjsGrid>
</div>
</div>
</div>
}
@code {
public class FlattenDeviceModel : DeviceModel
{
public string Ledcolor
{
get { return DeviceParameters["led_color"]; }
set
{
}
}
public string Powerstate
{
get { return DeviceParameters["power_state"]; }
set
{
}
}
public string Powerofftime
{
get
{
if (DeviceParameters.ContainsKey("power_off_time"))
{
return DeviceParameters["power_off_time"];
}
else
{
return null;
}
}
set
{
}
}
}
...
}
|
|
<EjsGrid ID="DeviceGrid" @ref="DeviceGrid" DataSource="Devices" ...>
<GridEvents OnToolbarClick="ToolbarClick" TValue="DeviceModel"></GridEvents>
...
</EjsGrid>
@code
{
public List<DeviceModel> Devices = new List<DeviceModel>()
{
...
};
private EjsGrid<DeviceModel> DeviceGrid;
public List<System.Dynamic.ExpandoObject> gridData = new List<System.Dynamic.ExpandoObject>(); //Define the ExpandoObject which is the custom DataSource
public void ToolbarClick(Syncfusion.EJ2.Blazor.Navigations.ClickEventArgs args)
{
if (args.Item.Id == "DeviceGrid_excelexport")
{
gridData = new List<System.Dynamic.ExpandoObject>(); //Empty the ExpandoObject
List<string> cols = new List<string>(); //Define the custom columns field names List
foreach (var device in Devices)
{
var x = new ExpandoObject() as IDictionary<string, Object>;
foreach (var parameter in device.DeviceParameters)
{
x.Add(parameter.Key, parameter.Value); //Fetch the key and value from Dictionary(DeviceParameters) and add to the ExpandoObject
if (!cols.Contains(parameter.Key))
{
cols.Add(parameter.Key); //Add the field information to cols List
}
}
////Do the same for all other defined fields(like Id, Name etc.) as like the above Dictionary(DeviceParameters) value.
Concept is to flatten the DataSource bind to Grid, and generate a custom ExpandoObject based on the DataSource provided for Grid. And generate a custom column list based on the generated ExpandoObject.
...
gridData.Add(x as ExpandoObject); //Add the dynamically generated Objects to the List of ExpandoObject
}
ExcelExportProperties excelprop = new ExcelExportProperties(); //generate instance for ExcelExportProperties
excelprop.DataSource = gridData; //Assign the generated custom ExpandoObject to DataSource property
var columns = new List<GridColumn>();
foreach(var c in cols)
{
var additionalColumn = new GridColumn() { Field = c, Width = "100px" };
columns.Add(additionalColumn); //Add the columns from the “cols List” to List<GridColumn>
}
excelprop.Columns = columns; //Assign the custom columns generated based on the ExpandoObject to Columns property
DeviceGrid.ExcelExport(excelprop); //Export the Grid with the assigned custom Columns and DataSource
}
...
}
...
}
|
|
public async Task ExcelExport()
{
...
ExcelExportProperties ExportProperties = new ExcelExportProperties();
//You can generate custom columns and assign to ExportProperties.Columns to perform custom column exporting
ExportProperties.Columns = Exportcols;
await this.DefaultGrid.ExcelExport(ExportProperties);
}
|
|
public async Task ExcelExport()
{
var excelProp = new ExcelExportProperties();
var columns = new List<GridColumn>();
//You can also use GetColumns methods to get all existing grid columns
#pragma warning disable BL0005
var additionalColumn = new GridColumn() { Field = "ShipCountry", Width = "100px" };
#pragma warning restore BL0005
columns.Add(additionalColumn);
excelProp.Columns = columns;
await Grid.ExcelExport(excelProp);
}
|
Hello,
do you have solution for including template column in Excel export for EJ1?
The template column I'd like to export is much simpler than Patric's, it's calculated from some other columns. (as an illustration, having columns Amount and Quantity, template column would be TotalAmount)
I can't just add property to datasource's model class because I also use batch edit so columns can be changed after initial load, that's why I need to have a template column.
I don't completely understand the documentation. Is templateInfo function obligatory or it's just used for some additional modifications?
I'd like the values from template column to be transferred to controller, not to calculate them again on server side. Is that possible?
|
[HomeController.cs]
public void ExportToExcel(string GridModel) {
ExcelExport exp = new ExcelExport();
var DataSource = OrdersDetails.GetAllRecords();
GridProperties obj = (GridProperties)Syncfusion.JavaScript.Utils.DeserializeToModel(typeof(GridProperties), GridModel);
exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, true, "flat-saffron", true);
}
|
I use Export method with just three parameters, of which the third is GridExcelExport object in which I define properties like FileName, ExcelVersion, Theme and IsTemplateColumnIncluded. So my excel export action looks something like this:
public ActionResult ExcelExport(string GridModel)
{
ExcelExport exp = new ExcelExport();
GridProperties gridProp = ConvertGridObject(GridModel);
GridExcelExport excelExp = new GridExcelExport();
excelExp.FileName = "Export" + DateTime.Now.ToString("_dd.MM.yyyy.") + ".xlsx";
excelExp.Excelversion = ExcelVersion.Excel2016;
excelExp.Theme = "flat-saffron";
excelExp.IsTemplateColumnIncluded = true;
gridProp.Columns.RemoveAt(...)
gridProp.Columns[12].HeaderText = (...)
using (SqlConnection connection = new SqlConnection(Global.SQLConnection))
{
gridProp.Columns[7].DataSource = (...)
}
gridProp.ServerExcelQueryCellInfo = QueryCellInfo;
gridProp.Columns.Add(new Column() (...)
var ds = JsonConvert.DeserializeObject(gridProp.DataSource.ToString());
return exp.Export(gridProp, ds.dataSource.json, excelExp);
}
When done this way, I get the template column in the excel file, but it's empty (there is just header name in that column).
I've tried do it your way, but I get multiple errors.
I've created incident 344543 and shared details there.