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

Export to excel

Hi,
I have a grid and I want to export to excel. Accordin to your sample I have written the following code:
        public void ExportToExcel(string GridModel)
        {
            ProductionRepository pdr = new ProductionRepository();
            ExcelExport exp = new ExcelExport();
            int iSelYear = DateTime.Today.Year;
            //if (!string.IsNullOrEmpty(SelectedYear))
            //    iSelYear = Convert.ToInt32(SelectedYear);
            IEnumerable<Production> pro = pdr.GetProductionsbyYear(iSelYear);
            List<ProductionViewModels> list = ProductionModels(pro);
            var DataSource = list;
            GridProperties obj = ConvertGridObject(GridModel);
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
        }

        private GridProperties ConvertGridObject(string gridProperty)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();
            IEnumerable div = (IEnumerable)serializer.Deserialize(gridProperty, typeof(IEnumerable));
            GridProperties gridProp = new GridProperties();
            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;
        }
When I click on the export button I get a System.NullReferenceException at
exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");

The stack trace is
[NullReferenceException: Riferimento a un oggetto non impostato su un'istanza di oggetto.]
   Syncfusion.EJ.Export.GridExcelExport.ProcessRecordCell(Object row, Column column) +874
   Syncfusion.EJ.Export.<>c__DisplayClasse.<ProcessRecordRow>b__d(Column column) +421
   System.Collections.Generic.List`1.ForEach(Action`1 action) +11010045
   Syncfusion.EJ.Export.GridExcelExport.ProcessRecordRow(Object row) +282
   Syncfusion.EJ.Export.GridExcelExport.RenderRecord() +222
   Syncfusion.EJ.Export.GridExcelExport.ProcessGridContents() +275
   Syncfusion.EJ.Export.GridExcelExport.IterateElements() +539
   Syncfusion.EJ.Export.GridExcelExport.ExportHandler() +50
   Syncfusion.EJ.Export.GridExcelExport.ExecuteResult(GridProperties GridModel, IEnumerable dataSource) +553
   Syncfusion.EJ.Export.GridExcelExport.ExportHelper(GridProperties gridModel, IEnumerable dataSource) +1000
   Syncfusion.EJ.Export.GridExcelExport.Export(GridProperties gridModel, IEnumerable dataSource, Boolean multipleExport) +71
   Syncfusion.EJ.Export.ExcelExport.Export(GridProperties gridmaodel, IEnumerable datasource, String excelname, ExcelVersion excelversion, Boolean isHideColumnIncude, Boolean isTemplateColumnIclude, String theme) +213

Thank you for your help.
Best regards,
Stefania Solari


3 Replies

JK Jayaprakash Kamaraj Syncfusion Team March 21, 2016 06:54 AM UTC

Hi Stefania,
Thanks for contacting Syncfusion support.
We have created sample with your code example, but we were unable to reproduce the issue. We suspect this issue occurs when a column field is not defined in the Grid dataSource, it is obtained as null value and hence throws a null exception. Please refer to the following knowledgebase document.

https://www.syncfusion.com/kb/3018/Null-Exception-while-exporting

 If you still face the issue, please share the following information to find the cause of the issue.

1.       Client side and server side full code example.

2.       Are you performing any Grid operations like filtering\searching before exporting?

3.       Essential studio version.

4.       An issue reproducing sample if possible or sample hosted link or replicate the issue in the following sample.

Sample:  http://www.syncfusion.com/downloads/support/forum/123454/ze/SyncfusionMvcApplication60862136599


Regards,


Jayaprakash K.



ME Megatron April 4, 2016 10:42 PM UTC

Hi, I have the same exact issue, it was working previous 13.4.0.53 build its not working in 4.0.63 after update service pack dll's.

Also when I add the export, its telling me the PDF export and excel export is supported in many SF dlls - PDF base, export - which version is the correct version please


Server Error in '/' Application.

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 171:            GridProperties obj = ConvertGridObject(GridModel);
Line 172:
Line 173:            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, false, "flat-saffron");
Line 174:
Line 175:        }

Source File: C:\users\mega\gardeningAdmin\GridController.cs    Line: 113

Stack Trace:


[NullReferenceException: Object reference not set to an instance of an object.]
   Syncfusion.EJ.Export.GridExcelExport.ProcessRecordCell(Object row, Column column) +422
   Syncfusion.EJ.Export.<>c__DisplayClasse.<ProcessRecordRow>b__d(Column column) +230
   System.Collections.Generic.List`1.ForEach(Action`1 action) +11439282
   Syncfusion.EJ.Export.GridExcelExport.ProcessRecordRow(Object row) +197
   Syncfusion.EJ.Export.GridExcelExport.RenderRecord() +121
   Syncfusion.EJ.Export.GridExcelExport.ProcessGridContents() +165
   Syncfusion.EJ.Export.GridExcelExport.IterateElements() +338


JK Jayaprakash Kamaraj Syncfusion Team April 5, 2016 08:51 AM UTC

Hi Megatron,

Query1 : Also when I add the export, its telling me the PDF export and excel export is supported in many SF dlls - PDF base, export - which version is the correct version pleas

In the 13.4.0.63 version , we need to refer (Syncfusion.Pdf.Base, Syncfusion.XlsIO.Base and Syncfusion.DocIO.Base) corresponding version DLL’s and also ensure the .NET framework version. We have attached code example and screenshot of all the DLL reference for 4.5 framework.

Please refer to the following code example and screenshot.

Web.config


     <assemblies>

        <add assembly="Syncfusion.EJ, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

        <add assembly="Syncfusion.Compression.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

        <add assembly="Syncfusion.EJ.MVC, Version=13.4500.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

        <add assembly="Syncfusion.Linq.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

        <add assembly="Syncfusion.XlsIO.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

        <add assembly="Syncfusion.Olap.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

      <add assembly="Syncfusion.Pdf.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />

         <add assembly="Syncfusion.DocIO.Base, Version=13.4450.0.63, Culture=neutral, PublicKeyToken=3d67ed1f87d44c89" />
      </assemblies>




Query2: Object reference not set to an instance of an object.

We were unable to reproduce the issue. But we suspect that the issue occurs when a column field is not defined in the Grid dataSource and obtained as null value, and hence throws a null exception.

Refer to the following knowledgebase document.

https://www.syncfusion.com/kb/3018/Null-Exception-while-exporting


 If you still face the issue, share the following information to find the cause of the issue.

1.       Client side and server side full code example.

2.       Are you performing any Grid operations like filtering\searching before exporting?

3.       Essential studio version.

4.       An issue reproducing sample if possible or sample hosted link or replicate the issue in the following sample.

http://www.syncfusion.com/downloads/support/forum/123454/ze/SyncfusionMvcApplication60873316834


Regards,


Jayaprakash K.


Loader.
Up arrow icon