Search results

Exporting Grid in Server in JavaScript Grid control

29 Sep 2022 / 4 minutes to read

The Grid have an option to export the data to Excel in server side using Grid server export library.

Server dependencies

The Server side export functionality is shipped in the Syncfusion.EJ2.GridExport package, which is available in Essential Studio and nuget.org.The following list of dependencies is required for Grid server side Excel exporting action.

  • Syncfusion.EJ2
  • Syncfusion.EJ2.GridExport
  • Syncfusion.Compression.Base
  • Syncfusion.XlsIO.Base

Server configuration

The following code snippets shows server configuration using ASP.NET MVC Controller Action.

To Export the Grid in server side, You need to call the serverExcelExport method for passing the Grid properties to server exporting action.

Copied to clipboard
public ActionResult ExcelExport(string gridModel)
{
GridExcelExport exp = new GridExcelExport();
Grid gridProperty = ConvertGridObject(gridModel);
return exp.ExcelExport<OrdersDetails>(gridProperty, OrderRepository.GetAllRecords());
}

private Grid ConvertGridObject(string gridProperty)
{
Grid GridModel = (Grid)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(Grid));
GridColumnModel cols = (GridColumnModel)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(GridColumnModel));
GridModel.Columns = cols.columns;
return GridModel;
}

public class GridColumnModel
{
public List<GridColumn> columns { get; set; }
}

public ActionResult DataSource(DataManager dm)
{
var DataSource = OrderRepository.GetAllRecords();
DataResult result = new DataResult();
result.result = DataSource.Skip(dm.Skip).Take(dm.Take).ToList();
result.count = result.result.Count;
return Json(result, JsonRequestBehavior.AllowGet);
}
Copied to clipboard
import { Grid, Toolbar } from '@syncfusion/ej2-grids';
import { DataManager, UrlAdaptor } from '@syncfusion/ej2-data';
Grid.Inject(Toolbar);

let data: DataManager = new DataManager({
url: "Home/DataSource",
adaptor: new UrlAdaptor
});

let grid: Grid = new Grid({
dataSource: data,
toolbar: ['ExcelExport'],
columns: [
    { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 100 },
    { field: 'CustomerID', headerText: 'Customer ID', width: 120 },
    { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 120, format: 'C2' },
    { field: 'ShipCountry', headerText: 'Ship Country', width: 150 }
],
height: 265
});
grid.appendTo('#Grid');

grid.toolbarClick = (args: Object) => {
if (args['item'].id === 'Grid_excelexport') {
    grid.serverExcelExport("Home/ExcelExport");
}
}

Note: Refer to the GitHub sample for quick implementation and testing from here.

CSV export in server side

You can export the Grid to CSV format by using the serverCsvExport method which will pass the Grid properties to server.

In the below demo, we have invoked the above method inside the toolbarClick event. In server side, we have deserialized the Grid properties and passed to the CsvExport method which will export the properties to CSV format.

Copied to clipboard
public ActionResult CsvGridExport(string gridModel)
{
GridExcelExport exp = new GridExcelExport();
Grid gridProperty = ConvertGridObject(gridModel);
return exp.CsvExport<OrdersDetails>(gridProperty, OrderRepository.GetAllRecords());
}

private Grid ConvertGridObject(string gridProperty)
{
Grid GridModel = (Grid)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(Grid));
GridColumnModel cols = (GridColumnModel)Newtonsoft.Json.JsonConvert.DeserializeObject(gridProperty, typeof(GridColumnModel));
GridModel.Columns = cols.columns;
return GridModel;
}

public ActionResult DataSource(DataManager dm)
{
var DataSource = OrderRepository.GetAllRecords();
DataResult result = new DataResult();
result.result = DataSource.Skip(dm.Skip).Take(dm.Take).ToList();
result.count = result.result.Count;
return Json(result, JsonRequestBehavior.AllowGet);
}
Copied to clipboard
import { Grid, Toolbar } from '@syncfusion/ej2-grids';
import { DataManager, UrlAdaptor } from '@syncfusion/ej2-data';
Grid.Inject(Toolbar);

let data: DataManager = new DataManager({
url: "Home/DataSource",
adaptor: new UrlAdaptor
});

let grid: Grid = new Grid({
dataSource: data,
toolbar: ['CsvExport'],
columns: [
    { field: 'OrderID', headerText: 'Order ID', textAlign: 'Right', width: 100 },
    { field: 'CustomerID', headerText: 'Customer ID', width: 120 },
    { field: 'Freight', headerText: 'Freight', textAlign: 'Right', width: 120, format: 'C2' },
    { field: 'ShipCountry', headerText: 'Ship Country', width: 150 }
],
height: 265
});
grid.appendTo('#Grid');

grid.toolbarClick = (args: Object) => {
if (args['item'].id === 'Grid_csvexport') {
    grid.serverCsvExport("Home/CsvGridExport");
}
}

Rotate a header text to a certain degree in the exported grid on the server side

The DataGrid has support to customize the column header styles such as changing text orientation, the font color, and so on in the exported Excel file. To achieve this requirement, use the ServerExcelHeaderQueryCellInfo event of the Grid.

The ServerExcelHeaderQueryCellInfo will be triggered when creating a column header for the excel document to be exported in the server side. Customize the column header in this event.

In the following demo, using the HeaderCellRotate method of the GridExcelExport class in the ServerExcelHeaderQueryCellInfo event, you can rotate the header text of the column header in the excel exported document.

Copied to clipboard
public ActionResult ExcelExport(string gridModel)
{
GridExcelExport exp = new GridExcelExport();
Grid gridProperty = ConvertGridObject(gridModel);
gridProperty.ServerExcelHeaderQueryCellInfo = ExcelHeaderQueryCellInfo;
IEnumerable data = Utils.DataTableToJson(dt);
var result = exp.ExcelExport<dynamic>(gridProperty, data);
return result;
}

private void ExcelHeaderQueryCellInfo(object excel)
{
ServerExcelHeaderQueryCellInfoEventArgs name = (ServerExcelHeaderQueryCellInfoEventArgs)excel;
headerValues.Add(name.Column.HeaderText);
var longestString = headerValues.Where(s => s.Length == headerValues.Max(m => m.Length)).First();
GridExcelExport exp = new GridExcelExport();
var size = exp.ExcelTextSize(name.Style.Font.FontName, (float)name.Style.Font.Size, longestString);
name.Cell.RowHeight = size.Width;
exp.HeaderCellRotate(name, 45); // Give the rotate degree value by the user.  
name.Style.Borders.LineStyle = Syncfusion.XlsIO.ExcelLineStyle.None;
}