We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date

Excel Multiple Export


Hi,

I have a page with four grids, and I wan to export all of them.

I the first grid

 @(Html.EJ().Grid<Flujo>("Cashflow")
            .Datasource(ds => ds.Json((IEnumerable<Tupla>)ViewBag.Cuentas).UpdateURL("FlujoUpdate").Adaptor(AdaptorType.RemoteSaveAdaptor))          
            .CssClass("CustomCss")
            .AllowMultipleExporting()          
             .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
             {
                 items.AddTool(ToolBarItems.ExcelExport);
             }))            
           
            })
            .Columns(col =>
            {
                col.Field("Empresa").Visible(false).Add();
                col.Field("Unidad").Visible(false).Add();
                col.Field("Year").Visible(false).Add();
                col.Field("SemDesde").Visible(false).Add();
                col.Field("SemHasta").Visible(false).Add();
                col.Field("CambioUSD").Visible(false).Add();
                col.Field("CambioEUR").Visible(false).Add();
                col.Field("Key").HeaderText("Semana").IsPrimaryKey(true).TextAlign(TextAlign.Center).Width(75).Add();
                col.Field("CXC").HeaderText("Cuentas por Cobrar").Template("#cxcTemplate").TextAlign(TextAlign.Center).AllowEditing(false).Width(110).Add();
                col.Field("CXP").HeaderText("Cuentas por Pagar").Template("#cxpTemplate").TextAlign(TextAlign.Center).AllowEditing(false).Width(110).Add();
                col.Field("Gastoss").HeaderText("Gastos de Operaciones").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("Bancos").HeaderText("Bancos").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").AllowEditing(false).Add();
                col.Field("TotalSemana").HeaderText("Total").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").AllowEditing(false).Add();
            })
            .Mappers(map => map.ExportToExcelAction("MultipleExportToExcel"))
            .ClientSideEvents(eve => { eve.TemplateRefresh("template").ActionComplete("complete").ActionBegin("begin").EndEdit("endEdit").ToolbarClick("OnToolbarClick"); })
        )

4 Replies

DA Dayne October 18, 2016 02:30 PM UTC

Hi,

I haven´t finished the request, when I send it. Sorry

I have a page with three grids, and I wan to export all of them.

This is the first grid

@(Html.EJ().Grid<Flujo>("Cashflow")
            .Datasource(ds => ds.Json((IEnumerable<Tupla>)ViewBag.Cuentas).UpdateURL("FlujoUpdate").Adaptor(AdaptorType.RemoteSaveAdaptor))          
            .CssClass("CustomCss")
            .AllowMultipleExporting()          
             .ToolbarSettings(toolBar => toolBar.ShowToolbar().ToolbarItems(items =>
             {
                 items.AddTool(ToolBarItems.ExcelExport);
             }))            
           
            })
            .Columns(col =>
            {
                col.Field("Empresa").Visible(false).Add();
                col.Field("Unidad").Visible(false).Add();
                col.Field("Year").Visible(false).Add();
                col.Field("SemDesde").Visible(false).Add();
                col.Field("SemHasta").Visible(false).Add();
                col.Field("CambioUSD").Visible(false).Add();
                col.Field("CambioEUR").Visible(false).Add();
                col.Field("Key").HeaderText("Semana").IsPrimaryKey(true).TextAlign(TextAlign.Center).Width(75).Add();
                col.Field("CXC").HeaderText("Cuentas por Cobrar").Template("#cxcTemplate").TextAlign(TextAlign.Center).AllowEditing(false).Width(110).Add();
                col.Field("CXP").HeaderText("Cuentas por Pagar").Template("#cxpTemplate").TextAlign(TextAlign.Center).AllowEditing(false).Width(110).Add();
                col.Field("Gastoss").HeaderText("Gastos de Operaciones").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("Bancos").HeaderText("Bancos").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").AllowEditing(false).Add();
                col.Field("TotalSemana").HeaderText("Total").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").AllowEditing(false).Add();
            })
            .Mappers(map => map.ExportToExcelAction("MultipleExportToExcel"))
            .ClientSideEvents(eve => { eve.TemplateRefresh("template").ActionComplete("complete").ActionBegin("begin").EndEdit("endEdit").ToolbarClick("OnToolbarClick"); })
        )
 

Second grid and the Third grid

@(Html.EJ().Grid<Flujo>("CashflowMX")
            .Datasource((IEnumerable<Tupla>)ViewBag.CuentasMXN)           
            .Locale("es-MX")     
            .Columns(col =>
            {
                col.Field("Key").HeaderText("Semana").IsPrimaryKey(true).TextAlign(TextAlign.Center).Width(75).Add();
                col.Field("CXC").HeaderText("Cuentas por Cobrar").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("CXP").HeaderText("Cuentas por Pagar").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();              
                col.Field("Gastoss").HeaderText("Gastos de Operaciones").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("Bancos").HeaderText("Bancos").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("TotalSemana").HeaderText("Total").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
            })
           
        )


@(Html.EJ().Grid<Flujo>("CashflowUS")
            .Datasource((IEnumerable<Tupla>)ViewBag.CuentasUS)           
            .Locale("es-MX")     
            .Columns(col =>
            {
                col.Field("Key").HeaderText("Semana").IsPrimaryKey(true).TextAlign(TextAlign.Center).Width(75).Add();
                col.Field("CXC").HeaderText("Cuentas por Cobrar").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("CXP").HeaderText("Cuentas por Pagar").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();              
                col.Field("Gastoss").HeaderText("Gastos de Operaciones").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("Bancos").HeaderText("Bancos").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
                col.Field("TotalSemana").HeaderText("Total").TextAlign(TextAlign.Center).Width(75).Format("{0:C}").Add();
            })
           
        )

 
In javascript

function OnToolbarClick(args) {           
                if (args.itemName.indexOf("Export") > -1) {
                   //I do this beacuse I need this parameters to filter the datasourse, this filter are extenals to the grids
                    this.model["empresa"] = $("#empresa > option:selected").attr("value");
                    this.model["unidad"] = $("#account > option:selected").attr("value");
                    
                }
 }   
 
The thing is that in the Controller the function public void MultipleExportToExcel(string[] GridModel)  the parameter GridModel is null, so the question is, did I miss anything? There is any other way to achive this? 
Keep in mind, that I need to change the font of the excel, colors, etc.

Best regards



PK Prasanna Kumar Viswanathan Syncfusion Team October 19, 2016 02:26 PM UTC

Hi Dayne, 
  
Thanks for contacting Syncfusion support. 
  
  
        Queries 
                                              Response 
  
I have a page with three grids, and I want to export all of them.” 
  
  
To export to excel with three grid, use the below code example 
  
  
public void MultipleExportToExcel(string[] GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            var EmployeeData = new NorthwindDataContext().EmployeeViews.Take(5).ToList(); 
            var OrderData = new NorthwindDataContext().OrdersViews.Take(5).ToList(); 
            var DetailedData = BindDataSource(); 
            bool initial = true; 
            IWorkbook book = null; 
            IWorkbook book2 = null; 
            for (int i =0 ; i < GridModel.Count(); i++) 
            { 
                string gridProperty = GridModel[i]; 
                GridProperties gridProp = ConvertObject(gridProperty); 
                if (initial) 
                { 
                    gridProp.Locale = ""; 
                    book = exp.Export(gridProp, EmployeeData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-saffron", true); 
                    initial = false; 
                } 
                else 
                { 
                    if (i != 2) 
                    { 
                        book2 = exp.Export(gridProp, OrderData, "Export2.xlsx", ExcelVersion.Excel2010, true, true, "flat-saffron", true); 
  
                        IWorksheet booksheet = book.Worksheets[0]; 
  
                        IWorksheet book2worksheet = book2.Worksheets[0]; 
  
                        IRange sourceRange = booksheet.UsedRange; 
  
                        IRange destinationRange = book2worksheet.UsedRange; 
  
                      //  //Copying (90 rows) from Source to Destination worksheet. 
  
                        destinationRange.CopyTo(booksheet.Range[sourceRange.LastRow + 2, 1]); 
                                           
                    } 
                    else 
                        exp.Export(gridProp, DetailedData, "Export.xlsx", ExcelVersion.Excel2010, true, true, "flat-saffron", false, book, MultipleExportType.AppendToSheet, "Third Grid"); 
                } 
            } 
        } 
  
  
  
I need to change the font of the excel, colors, etc.” 
  
To customize the style for the exported grid, use the AutoFormat class. With the autoFormat class, you can provide required color to the grid content, alt row background or border color. 
  
Find the code example: 
  
  
private GridProperties ConvertObject(string gridProperty) 
        { 
            -------------- 
           GridProperties gridProp = new GridProperties(); 
            GridExtensions ext = new GridExtensions(); 
            AutoFormat auto = new AutoFormat(); 
            ext.SetTheme(auto, "flat-saffron"); 
            auto.FontFamily = "Calibri"; 
            auto.GContentFontColor = Color.Blue; 
            gridProp.AutoFormat = auto; 
            foreach (KeyValuePair<string, object> ds in div) 
            { 
                var property = gridProp.GetType().GetProperty(ds.Key, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase); 
                if (property != null) 
                { 
                    Type type = property.PropertyType; 
                    string serialize = serializer.Serialize(ds.Value); 
                    object value = serializer.Deserialize(serialize, type); 
                    property.SetValue(gridProp, value, null); 
                } 
            } 
            return gridProp; 
        } 
  
For more information refer the below document. 
  
  
  
“To pass additional Parameter” 
  
  
We have already discussed about this requirement in the below knowledgebase documentation. 
  
  
Refer the above documentation and still if you face any issue, please get back to us.  
  
  
  
Regards, 
Prasanna Kumar N.S.V 
 



DA Dayne October 19, 2016 04:13 PM UTC

Hi, 

Like I previously said  

"The thing is that in the Controller the function public void MultipleExportToExcel(string[] GridModel)  the parameter GridModel is null, so the question is, did I miss anything?"

So, I wasn't able to get any Grid Properties. But I solve it, in the javascript section, I has a bad reference, fix this reference, solve the issue.

But thanks


PK Prasanna Kumar Viswanathan Syncfusion Team October 20, 2016 10:03 AM UTC

Hi Dayne, 

We are happy to hear that you issue has been resolved. 

Please let me know if you need any further assistance. 

Regards, 
Prasanna Kumar N.S.V 


Loader.
Up arrow icon