Удалите часть «Excel» и добавьте
.ToolBar(toolBar => { toolBar.Custom().Text("Export To Excel")
.HtmlAttributes(new { @class = "export" })
.Url(Url.Action("ExcelExport", "Grid")); })
, затем в контроллере:
public ActionResult ExcelExport([DataSourceRequest]DataSourceRequest request
{
var workbook = new HSSFWorkbook();
//Create new Excel sheet
var sheet = workbook.CreateSheet();
//(Optional) set the width of the columns
sheet.SetColumnWidth(0, 10 * 256);
sheet.SetColumnWidth(1, 50 * 256);
sheet.SetColumnWidth(2, 50 * 256);
sheet.SetColumnWidth(3, 50 * 256);
//Create a header row
var headerRow = sheet.CreateRow(0);
//Set the column names in the header row
headerRow.CreateCell(0).SetCellValue("Product ID");
headerRow.CreateCell(1).SetCellValue("Product Name");
headerRow.CreateCell(2).SetCellValue("Unit Price");
headerRow.CreateCell(3).SetCellValue("Quantity Per Unit");
//(Optional) freeze the header row so it is not scrolled
sheet.CreateFreezePane(0, 1, 0, 1);
var models = _priceRepository.Get().ToDataSourceResult(request).Data;
int rowNumber = 1;
//Populate the sheet with values from the grid data
foreach (RentPriceModel model in models)
{
//Create a new row
var row = sheet.CreateRow(rowNumber++);
//Set values for the cells
row.CreateCell(0).SetCellValue(model.Id);
row.CreateCell(1).SetCellValue(model.Branch.Name);
row.CreateCell(2).SetCellValue(model.CarClass.Name);
row.CreateCell(3).SetCellValue(model.Interval);
}
//Write the workbook to a memory stream
MemoryStream output = new MemoryStream();
workbook.Write(output);
//Return the result to the end user
return File(output.ToArray(), //The binary data of the XLS file
"application/vnd.ms-excel", //MIME type of Excel files
"GridExcelExport.xls");
}`
Обратите внимание, что я использую свои пользовательские модели, а также эту библиотеку