Exporting to Excel from standard ASP.net GRidView

Hi. I am using the following code to try to export data to Excel from a standard ASP.net GridView but am getting the error in the attached image.

Image1.png

        protected void btnExportExcel_Click(object sender, EventArgs e)

        {

            ExportExcel();

        }


        protected void ExportExcel()

        {

            //Initialize the Excel Engine

            using (ExcelEngine excelEngine = new ExcelEngine())

            {

                //Initialize GridView control


                GridView gridView = this.GridView1;


                //Initialize Application

                IApplication application = excelEngine.Excel;


                //Set default version for application.

                application.DefaultVersion = ExcelVersion.Excel2013;


                //Create a new workbook

                IWorkbook workbook = application.Workbooks.Create(1);


                //Accessing first worksheet in the workbook

                IWorksheet worksheet = workbook.Worksheets[0];


                //Import data from GridView control

                worksheet.ImportGridView(gridView, 1, 1, true, true);


                //Save the workbook to disk in xlsx format

                workbook.SaveAs("Sample.xlsx", Response, ExcelDownloadType.Open, ExcelHttpContentType.Excel2013);

            }

        }


1 Reply

FS Farveen Sulthana Thameeztheen Basha Syncfusion Team March 30, 2022 04:10 PM UTC

Hi Murray,


We have checked your query and we have already discussed about Exporting feature for Grid in below documentation and Demo links. Please refer to the below:-

https://help.syncfusion.com/aspnet/grid/exporting

https://asp.syncfusion.com/demos/web/grid/excelexporting.aspx


KB link:- https://www.syncfusion.com/kb/4335/how-to-export-grid-data-to-excel-word-and-pdf-formats-on-external-action


After following the above still faced issue, share us the following details.


  1. Complete Grid code example.(both Client and Server code).
  2. StackTrace of the Exception you have faced.
  3. Video Demo to replicate the issue.


Regards,
Farveen sulthana T


Loader.
Up arrow icon