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
close icon

Exporting the Grid to Excel

By assigning the toolbarClick event the ExportToExcel Action as seen in the Demo's can be overriden to allow Grid's binding to Remote data where the remote data source is filtered via parameters as in the following example:
C#:

 public ActionResult GetGridData(int? id, string GridModel)
        {
            var data = new List<Person>();
            if (!id.HasValue)
                id = 0;
            switch (id)
            {
                case 0:
                    data = GetPeopleOlderThan(30);
                    break;
                case 1:
                    data = GetPeopleYoungerThan(30);
                    break;
            }
            ExportToExcel(GridModel, payments);
            return Json("done");
        }

public void ExportToExcel(string GridModel, IEnumerable dataSource)
        {
            var exp = new ExcelExport();
            var DataSource = dataSource;
            var obj = ConvertGridObject(GridModel);

            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "default-theme");
        }
JavaScript for toolbarClick event handler: (The dropdown has 2 values, 0 and 1)
function toolbarClick(args) {

            switch (args.itemName) {
                case "Excel Export":
                    var dropDown = $("#status").data("ejDropDownList");;
                    var selectedValue = dropDown.getSelectedValue();
                    this.export("/People/GetGridData/"+ selectedValue);
                    args.cancel = true;
                    break;
            }
        }


The export function on the grid passes the GridModel string by default so by adding other parameters as a "prettyfied" route parameter allows for this to resolve correctly and to provide the desired result

5 Replies

SR Sellappandi Ramu Syncfusion Team February 17, 2015 09:39 AM UTC

Hi Cornel,

Thanks for using Syncfusion products.

We are unable to reproduce the reported errors from the provided code. Could you please share the following information to us to sort out the cause of the issue and provide you the solution as early as possible?

1. Share the replication procedure of the issue, which you faced while exporting.

2. Also update us what you have mentioned in the payments parameter given in the below table.

ExportToExcel(GridModel, payments);

return Json("done");

3. Where you are using the below mentioned data in your sample?

case 0:

        data = GetPeopleOlderThan(30);

        break;

case 1:

        data = GetPeopleYoungerThan(30);

        break;

4.  Share your full grid code snippet to us.

We have created a export sample using two parameters in ExportToExcel() method. One parameter is GridModel and another one is data. Please refer the following code snippet.

public ActionResult GetGridData(int? id, string GridModel)

        {

            var data = new List<Order>();

            IEnumerable res;

            if (!id.HasValue)

                id = 0;

            switch (id)

            {

                case 0:

                    data = new NORTHWNDEntities1().Orders.ToList();

                    break;

                case 1:

                    data = new NORTHWNDEntities1().Orders.ToList();

                    break;

            }

            ExportToExcel(GridModel, data);

           return Json("done");

        }

public void ExportToExcel(string GridModel, IEnumerable dataSource)

        {

            ExcelExport exp = new ExcelExport();

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, dataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");

        }

function toolbarClick(args) {

        switch (args.itemName) {

            case "Excel Export":

               var dropDown = $("#status").data("ejDropDownList");

               var selectedValue = dropDown.getSelectedValue();

                this.export("/Home/GetGridData/" + selectedValue);

                args.cancel = true;

                break;

        }

    }

Sample Link: http://www.syncfusion.com/downloads/support/directtrac/118253/Sample_118253315599700.zip

Please try the above sample if we misunderstood your query get back to us with the mentioned details.

Regards,

Sellappandi R


CO Cornel replied to Sellappandi Ramu February 18, 2015 04:48 PM UTC

Hi Cornel,

Thanks for using Syncfusion products.

We are unable to reproduce the reported errors from the provided code. Could you please share the following information to us to sort out the cause of the issue and provide you the solution as early as possible?

1. Share the replication procedure of the issue, which you faced while exporting.

2. Also update us what you have mentioned in the payments parameter given in the below table.

ExportToExcel(GridModel, payments);

return Json("done");

3. Where you are using the below mentioned data in your sample?

case 0:

        data = GetPeopleOlderThan(30);

        break;

case 1:

        data = GetPeopleYoungerThan(30);

        break;

4.  Share your full grid code snippet to us.

We have created a export sample using two parameters in ExportToExcel() method. One parameter is GridModel and another one is data. Please refer the following code snippet.

public ActionResult GetGridData(int? id, string GridModel)

        {

            var data = new List<Order>();

            IEnumerable res;

            if (!id.HasValue)

                id = 0;

            switch (id)

            {

                case 0:

                    data = new NORTHWNDEntities1().Orders.ToList();

                    break;

                case 1:

                    data = new NORTHWNDEntities1().Orders.ToList();

                    break;

            }

            ExportToExcel(GridModel, data);

           return Json("done");

        }

public void ExportToExcel(string GridModel, IEnumerable dataSource)

        {

            ExcelExport exp = new ExcelExport();

            GridProperties obj = ConvertGridObject(GridModel);

            exp.Export(obj, dataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");

        }

function toolbarClick(args) {

        switch (args.itemName) {

            case "Excel Export":

               var dropDown = $("#status").data("ejDropDownList");

               var selectedValue = dropDown.getSelectedValue();

                this.export("/Home/GetGridData/" + selectedValue);

                args.cancel = true;

                break;

        }

    }

Sample Link: http://www.syncfusion.com/downloads/support/directtrac/118253/Sample_118253315599700.zip

Please try the above sample if we misunderstood your query get back to us with the mentioned details.

Regards,

Sellappandi R

Yeah, you completely misunderstood. This basically some sample code to help users who want to achieve what I needed to do


SR Sellappandi Ramu Syncfusion Team February 19, 2015 08:34 AM UTC

Hi Cornel,

We are sorry to let you know that we are unable to get your requirements. Could you please provide the details regarding your achievements of this project. This will be helpful to sort out your requirements.

Regards,

Sellappandi R


EH Edgar H Velandia M June 11, 2015 11:28 PM UTC

Hi I'm looking to put header and footer titles, but in my case when ExportToPDF.

The process works well, but can not find how to add titles to the report.

                var fileName = String.Format("CarteraCliente_{0}_{1}.pdf", item.TRNCOD, DateTime.Now.ToShortDateString());
                PdfDocument document = new PdfDocument();
               
                // Document Properties
                document.PageSettings.Orientation = PdfPageOrientation.Landscape; //Sets landscape page orientation.
                document.DocumentInformation.Author = "Ehv.1";
                document.DocumentInformation.CreationDate = DateTime.Now;
                document.DocumentInformation.Title = "Estado Actual de Cartera";
                document.DocumentInformation.Subject = item.TRNNAM;

                // Export
                PdfExport exp = new PdfExport();         
                GridProperties obj = ConvertGridObject(GridModel);

                exp.Export(obj, DataSource, fileName, false, false, "gradient-azure", false, document, "Reporte de Cartera");

Thanks for help me please,

Edgar H. Velandia 



SR Sellappandi Ramu Syncfusion Team June 12, 2015 12:48 PM UTC

Hi Velandia,

We have created a sample to display the header and footer in PDF exporting. The sample can be downloaded from following location.

Sample Link: http://www.syncfusion.com/downloads/support/forum/118253/ze/EJGrid_118253252664437
 
In the above sample, we have added header and footer manually in exporting action. Please refer the following steps.

Step 1: We have saved the exported document to MemoryStream and edited that stored PDF file.

Step 2: Added the header and footer dynamically by using PdfPageTemplateElement.


Please refer the following code snippet.

        public void ExportToPdf(string GridModel)

        {

            PdfDocument document = new PdfDocument();

            document.DocumentInformation.Title = "Essential PDF Sample";     

            document.PageSettings.Orientation = PdfPageOrientation.Landscape;//Sets landscape page orientation.           

            PdfExport exp = new PdfExport();

            var DataSource = new NorthwindDataContext().OrdersViews.ToList();

            GridProperties obj = ConvertGridObject(GridModel);

            PdfDocument pdfdocument = exp.Export(obj, DataSource, "Export.pdf", true, true, "flat-saffron", true, document, document.DocumentInformation.Title);


            MemoryStream ms = new MemoryStream();

            pdfdocument.Save(ms);

            pdfdocument.Close(true);

            ms.Position = 0;

            PdfLoadedDocument ldDoc = new PdfLoadedDocument(ms);

            pdfdocument = new PdfDocument();

            pdfdocument.PageSettings.Margins.All = 0;

            //Get the templates from loadeddocument.

            for (var i = 0; i < ldDoc.Pages.Count; i++)

            {

                PdfTemplate template = ldDoc.Pages[i].CreateTemplate();

                pdfdocument.Pages.Add();

                pdfdocument.Pages[i].Graphics.DrawPdfTemplate(template, new PointF(0, 0));

            }

            RectangleF rect = new RectangleF(0, 0, pdfdocument.PageSettings.Width, 50);


            //Create a page template

            PdfPageTemplateElement footer = new PdfPageTemplateElement(rect);

            PdfPageTemplateElement header = new PdfPageTemplateElement(rect);

            Font f = new Font("Helvetica", 8, FontStyle.Regular);

            PdfFont font = new PdfTrueTypeFont(f, true);

            PdfSolidBrush brush = new PdfSolidBrush(Color.Gray);

            PdfPen pen = new PdfPen(Color.DarkBlue, 3f);

            f = new Font("Helvetica", 6, FontStyle.Bold);

            font = new PdfTrueTypeFont(f, true);

            PdfStringFormat format = new PdfStringFormat();

            format.Alignment = PdfTextAlignment.Center;

            format.LineAlignment = PdfVerticalAlignment.Middle;

            footer.Graphics.DrawString("Custom Text - Footer", font, brush, rect, format); //Add custom text to the header

            header.Graphics.DrawString("Custom Text - Header", font, brush, rect, format); //Add custom text to the footer



            pdfdocument.Template.Bottom = footer; //Append custom template to the document

            pdfdocument.Template.Top = header;

            pdfdocument.Save("Export.pdf", HttpContext.ApplicationInstance.Response,HttpReadType.Save);
        }


Please try the above sample and get back to us if you have any concerns.

Regards,
Sellappandi R

Loader.
Live Chat Icon For mobile
Up arrow icon