ASP.net
Awesome
Learn
Forum
Buy
Demos
Sign In
☾
☀
Switch to
Dark
Light
Mode
this site works best with javascript enabled
Ask Question
Using export to excel for grid with dynamic columns
Title:
B
I
{code}
?
Hi I am creating a grid with dynamic columns as :- public ActionResult GetGridItems(GridParams g, string search) { search = (search ?? "").ToLower(); ServiceReferences.ReportServiceReference.Report reportContract = null; ApplicationExceptionHandler.Handle(() => { ServiceProcessComponent.Use<ReportServiceClient>( reportService => { reportContract = reportService.GetContactsReport(); }); }); var columns = new List<Column>(); for (int i = 1; i < reportContract.ReportTable.Columns.Count; i++) { columns.Add(new Column { Header = reportContract.ReportTable.Columns[i].ToString(), ClientFormatFunc = "getVal(" + i + ")" }); } var items = new List<GridArrayRow>(); for (int i = 0; i < reportContract.ReportTable.Rows.Count; i++) { items.Add(new GridArrayRow { Id = reportContract.ReportTable.Rows[i][0].ToString(), Values = reportContract.ReportTable.Rows[i].ItemArray.Select(x => x.ToString()).ToArray() }); } g.Columns = columns.ToArray(); var model = new GridModelBuilder<GridArrayRow>(items.AsQueryable(), g).Build(); return Json(model, JsonRequestBehavior.AllowGet); } now I want to download this grid as excel using export to excel method, but in the given demo static columns have been mentioned [HttpPost] public ActionResult ExportGridToExcel(GridParams g) { var gridModel = BuildGridModel(g); var columns = new[] { "Id", "Person", "Food", "Price", "Date", "Location" }; var workbook = GridExcelBuilder.Build(gridModel, columns); var stream = new MemoryStream(); workbook.Write(stream); stream.Close(); return File(stream.ToArray(), "application/vnd.ms-excel", "lunches.xls"); } how to get the dynamic columns for this approch, and do I need to hit database again while calling export to excel method
Save Changes
Cancel
Tanuj Shrivastava
asked at 25 Jul 2016
you can see in the demo that Db.Lunches is called in BuildGridModel private method, and you'll have to generate "columns" yourself, and I think you'll also have to modify GridExcelBuilder ( the line: cell.SetCellValue(o.GetType().GetProperty(columns[j])) )
at 25 Jul 2016
Omu
Answers
please
Sign In
to leave an answer
By accessing this site, you agree to store cookies on your device and disclose information in accordance with our
cookie policy
and
privacy policy
.
OK