Add excel button in Grid.ts file.
namespace Serene.Northwind {
@Serenity.Decorators.registerClass()
@Serenity.Decorators.filterable()
export class CustomerGrid extends Serenity.EntityGrid<CustomerRow, any> {
protected getColumnsKey() { return "Northwind.Customer"; }
protected getDialogType() { return <any>CustomerDialog; }
protected getIdProperty() { return CustomerRow.idProperty; }
protected getLocalTextPrefix() { return CustomerRow.localTextPrefix; }
protected getService() { return CustomerService.baseUrl; }
constructor(container: JQuery) {
super(container);
}
getButtons() {
var buttons = super.getButtons();
buttons.push(Serene.Common.ExcelExportHelper.createToolButton({
grid: this,
onViewSubmit: () => this.onViewSubmit(),
service: 'Northwind/Customer/ListExcel',
separator: true
}));
buttons.push(Serene.Common.PdfExportHelper.createToolButton({
grid: this,
onViewSubmit: () => this.onViewSubmit()
}));
return buttons;
}
}
}
Add excel export server side mothis in Endpoint.cs.
namespace Serene.Northwind.Endpoints
{
using Serenity.Data;
using Serenity.Reporting;
using Serenity.Services;
using Serenity.Web;
using System;
using System.Data;
using System.Web.Mvc;
using MyRepository = Repositories.CustomerRepository;
using MyRow = Entities.CustomerRow;
[RoutePrefix("Services/Northwind/Customer"), Route("{action}")]
[ConnectionKey(typeof(MyRow)), ServiceAuthorize(typeof(MyRow))]
public class CustomerController : ServiceEndpoint
{
[HttpPost, AuthorizeCreate(typeof(MyRow))]
public SaveResponse Create(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MyRepository().Create(uow, request);
}
[HttpPost, AuthorizeUpdate(typeof(MyRow))]
public SaveResponse Update(IUnitOfWork uow, SaveRequest<MyRow> request)
{
return new MyRepository().Update(uow, request);
}
[HttpPost, AuthorizeDelete(typeof(MyRow))]
public DeleteResponse Delete(IUnitOfWork uow, DeleteRequest request)
{
return new MyRepository().Delete(uow, request);
}
public GetNextNumberResponse GetNextNumber(IDbConnection connection, GetNextNumberRequest request)
{
return new MyRepository().GetNextNumber(connection, request);
}
public RetrieveResponse<MyRow> Retrieve(IDbConnection connection, RetrieveRequest request)
{
return new MyRepository().Retrieve(connection, request);
}
public ListResponse<MyRow> List(IDbConnection connection, ListRequest request)
{
return new MyRepository().List(connection, request);
}
public FileContentResult ListExcel(IDbConnection connection, ListRequest request)
{
var data = List(connection, request).Entities;
var report = new DynamicDataReport(data, request.IncludeColumns, typeof(Columns.CustomerColumns));
var bytes = new ReportRepository().Render(report);
return ExcelContentResult.Create(bytes, "CustomerList_" +
DateTime.Now.ToString("yyyyMMdd_HHmmss") + ".xlsx");
}
}
}
Another way for add excel export button.
buttons.push(Common.ExcelExportHelper.createToolButton({
grid: this,
service: CustomerGrossSalesService.baseUrl + '/ListExcel',
onViewSubmit: () => this.onViewSubmit(),
separator: true
}));