private object QueryCellInfo(IQueryable arg1, SummaryColumn arg2){//How to sum a field here based on arg1
var rs = 100000;var dol = 0.017;return (rs * dol);}Thanks!
|
Client side:-
<ej:Grid ID="FlatGrid" runat="server" OnServerPdfExporting="FlatGrid_ServerPdfExporting" OnServerWordExporting="FlatGrid_ServerWordExporting" OnServerExcelExporting="FlatGrid_ServerExcelExporting">
<ToolbarSettings ShowToolbar="true" ToolbarItems="pdfExport”,”excelExport”,”wordExport"></ToolbarSettings>
<SummaryRows>
<ej:SummaryRow Title="Currency" >
. . .
</ej:SummaryRow>
</SummaryRows>
<Columns>
. . .
</Columns>
</ej:Grid>
Serverside:-
protected void FlatGrid_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e)
{
ExcelExport exp = new ExcelExport();
IWorkbook book = exp.Export(FlatGrid.Model, (IEnumerable)FlatGrid.DataSource, "Export.xlsx", ExcelVersion.Excel2010, true, false, "flat-lime", true);
IWorksheet worksheet = book.Worksheets[0];
//Get the index of the column
int inx = GetColIndex("CustomerID") + 1;
IConditionalFormats condition = worksheet[1, inx, worksheet.UsedRange.LastRow, inx].ConditionalFormats;
IConditionalFormat condition1 = condition.AddCondition();
condition1.FormatType = ExcelCFType.Formula;
//A1 refers to the first Column value i.e. OrderID
//We have taken Sum value
condition1.FirstFormula = "=Sum($A1,2)";
condition1.BackColor = ExcelKnownColors.Red;
book.SaveAs("Export.xlsx", ExcelSaveType.SaveAsXLS, System.Web.HttpContext.Current.Response, ExcelDownloadType.Open);
}
|
You don't have a custom summary in your Grid Example:
Please see the attached sample project (GridFeatures.aspx). link(https://cannypartner-my.sharepoint.com/personal/salamancaj_cannypartner_com/_layouts/15/guestaccess.aspx?docid=163795ede1e52480dac61ac39c32a642b&authkey=AZpWxw2KqDEHw3tpkI88ejM&e=2141e6b8a2d24e918d51f046d72bc477)
The example has a Grid which 3 summary columns, with 1 column being a custom summary. Custom summary is handled at the client side. When we want to export the data if you using the normal export code:
//Original Export
//ExcelExport exp = new ExcelExport();
//exp.Export(gdCatReview.Model, (IEnumerable)gdCatReview.DataSource, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-lime");
You get an exception, if you remove the custom summary column ( <ej:SummaryColumn SummaryType="Custom" CustomSummaryValue="GetCustomSummaryValue1" Format="{0:P2}" DisplayColumn="Freight" Prefix="PCT = " />), it exports normally.
this kb almost explains how to do it: https://www.syncfusion.com/kb/7452/how-to-export-grid-with-custom-summary
With the exception that it doesn't show how to calculate the value via the IQueryable.
If I do it the way you're suggesting, I'll have to do a lot of formatting on code behind to match the other SummaryColumns that export correctly.
Thank you,
Jose
| protected void gdCatReview_ServerExcelExporting(object sender, Syncfusion.JavaScript.Web.GridEventArgs e) { string ExportFileName = ("Export_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx"); ExcelExport exp = new ExcelExport(); GridExcelExport exp1 = new GridExcelExport(); ExcelEngine excel = new ExcelEngine(); IApplication application = excel.Excel; IWorkbook workbook = application.Workbooks.Create(2); exp1.QueryCustomSummaryInfo = QueryCellInfo; exp1.Theme = "default-theme"; . . . workbook.SaveAs(ExportFileName, ExcelSaveType.SaveAsXLS, Response, ExcelDownloadType.Open); } private object QueryCellInfo(IQueryable arg1, SummaryColumn arg2) { IQueryable<Orders> Items = arg1 as IQueryable<Orders>; var TotalSalesData = GetTotalSalesData1(Items); var TotalGPDollarData = GetTotalGPDollarData1(Items); return TotalGPDollarData / TotalSalesData; } public double GetTotalGPDollarData1(IQueryable<Orders> Items) { double TotalGPDollarData = 0; foreach (var item in Items) TotalGPDollarData += Convert.ToDouble(item.Freight); return TotalGPDollarData; } public double GetTotalSalesData1(IQueryable<Orders> Items) { double TotalSalesData = 0; foreach (var item in Items) TotalSalesData += Convert.ToDouble(item.EmployeeID); return TotalSalesData; } |
Thank you for the sample, I was able to get it working on my application.
Jose