Export on external action/button

Hi

i'm getting error: Object reference not set to an instance of an object on export method.

This is the JS button code:

 function exportToExcel() {
         var gridObj = $("#GridMaster").ejGrid("instance");
         gridObj.export("/Customer/ExportToExcel");
}

This is the controller code:

 public ActionResult ExportToExcel(string GridModel)
        {
           
                ExcelExport exp = new ExcelExport();
                GridProperties gridProp = ConvertGridObject(GridModel);
                GridExcelExport excelExp = new GridExcelExport
                {
                    FileName = "Export.xlsx",
                    Excelversion = Syncfusion.XlsIO.ExcelVersion.Excel2010,
                    Theme = "flat-saffron"
                };            
                var DataSource = _context.Customers.ToList();
             
                return exp.Export(gridProp, DataSource, excelExp);
            }

        private GridProperties ConvertGridObject(string gridProperty)
        {
            GridProperties gridProp = new GridProperties();
            gridProp = (GridProperties)JsonConvert.DeserializeObject(gridProperty, typeof(GridProperties));
            return gridProp;
        }
   
The GridModel is passed to the controller and the Datasource is populated so i don't know why i'm getting this error.


3 Replies

VA Venkatesh Ayothi Raman Syncfusion Team December 18, 2017 09:11 AM UTC

Hi Pratura, 

Thanks for using Syncfusion products. 

The issue was reproduced when the columns bound to the Grid are not defined in the Grid dataSource, and are passed to the exporting method in the server side. If so, we have already created a knowledge base documentation for this “null exception while exporting” and it can be referred from following link, 

If you still face the same issue then could you please provide the following details? 
1)      Provide Grid code example. 
2)      Are you using template column in Grid? If so, share the details. 
3)      Essential Studio version details. 
4)      Scenario to reproduce the reported issue. 
5)      Are you using foreign key column in the Grid? If so, we need to assign the foreign key data source before the calling the export method. Please refer to the following KB link for more information, 
6)      A sample is possible. 

 
Regards, 
Venkatesh Ayothiraman. 



PR Pratura December 19, 2017 09:15 AM UTC

Hi, Venkatesh 

thanks for the feedback.

The export is working now. I'm using the local datasource with
gridObj.ignoreOnExport.splice(gridObj.ignoreOnExport.indexOf('dataSource'), 1);

I now have other issues.

1. Since i'm using local Datasource i have to deserialize it to map it to the class.
    Then all date fields get one hour subtracted from the actual date in the grid.Datasource value because we're in the -1 UTC zone.
   Do you know how to correct this?

2. the date field(column), when exported in excel shows also the time as 00:00:00
    Is is possible to export the date field without the time?

3. Is it possible to set the width of every column the size of the content



VA Venkatesh Ayothi Raman Syncfusion Team December 20, 2017 01:48 PM UTC

Hi Pratura, 

Thanks for the update. 

Query #1:”Since i'm using local Datasource i have to deserialize it to map it to the class.Then all date fields get one hour subtracted from the actual date in the grid.Datasource value because we're in the -1 UTC zone.Do you know how to correct this?” 
This is already known issue, suppose application has been hosted in EST time zone and client may be in IST,GMT etc. In that case, date will differ from data base. Please refer to the following Help documentation and Code example for more information, 
<script type="text/javascript"> 
   . .  . 
    var serverTimeZoneDiff = -4.0  // if your server is in EDT time zone (UTC -4.0) (in hours) 
    var clientSideTimeZoneDiff = new Date().getTimezoneOffset() / 60; // get client time zone difference and convert it to hours; 
    ej.serverTimezoneOffset = serverTimeZoneDiff + clientSideTimeZoneDiff; 
 
. . . 
        </script> 

Query #2:”   Is is possible to export the date field without the time? 
Normally, the Grid export the date column based on the format which is mentioned in the columns property. So, if the date format is “{0:dd/MM/YYYY}” then exported date cell also same format. Because, we have performed the Export action based on the Grid model. If you still face the same issue, then could you please provide following details? 
1)      Share the JSON details of the first record in Grid. 
2)      Share the Grid code example. 
3)      Controller side code example of Export. 
4)      A sample if possible. 

Query #3:” Is it possible to set the width of every column the size of the content 
We suspect that do you want to set the column width based on the content of the cell. If so, we have built-in support of allowResizeToFit property in Grid. In that, Column width is set automatically based on the content or header text which is large. Please refer to the following Help documentation for more information, 
 

Regards, 
Venkatesh Ayothiraman. 


Loader.
Up arrow icon