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

IndexOutOfRangeException when Exporting ASP.NET MVC Grid to Excel With Column With Format Specified

I am exporting a grid to Excel and I am receiving the following error:
Index was outside the bounds of the array.
Exception Details: System.IndexOutOfRangeException: Index was outside the bounds of the array.

Source Error:
Line 133:            exp.Export(gridProperties, rows, fileName, ExcelVersion.Excel2010, true, false, "default-theme", false);

As soon as I remove the following column the error goes away:
col.Field(r => r.Symbol).Format("<a class='symbol'>{Symbol}</a>").Add();

How can I get the grid to export (the format in this case is not important).

Regards, Jeff





5 Replies

MS Mani Sankar Durai Syncfusion Team August 16, 2016 01:31 PM UTC

Hi Jeffrey, 

Thanks for contacting Syncfusion support, 

We are unable to reproduce the issue at our end. Please refer the following documentation and online link on how to export the grid, 
We have also prepared a sample that can be downloaded from the following location.  
 
Please share the following details, 
1)      Code example of Grid and code behind  
2)      If possible modify the attached sample and replicate the issue.  
The provided information will help us to analyze the issue and provide you the response as early as possible, 

Regards,
 
Manisankar Durai. 



JS Jeffrey Stone August 16, 2016 03:06 PM UTC

Manisankar,

I had trouble using the attached sample (wrong version of SQL Sever - I have 2014).

Would you be able to change the .Format("{0:C}") to .Format("<a class='symbol'>{Freight}</a>") ?

(I can confirm that .Format("{0:C}") works - as I use it on other columns - while .Format("<a class='symbol'>{Freight}</a>") does not work)

Regards, Jeff




SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 17, 2016 11:29 AM UTC

Hi Jeffrey, 

We suspect that you would like to place the value in the anchor tag. To define the html elements or other controls, we have template column support which will place any element. Refer to the following code example. 

@(Html.EJ().Grid<object>("FlatGrid") 
        .Datasource((IEnumerable<object>)ViewBag.dataSource) 
        .AllowPaging() 
              . . . 
                .Columns(col => 
            { 
                . . . 
                col.Field("Freight") 
                    .HeaderText("Freight") 
                    .Template("<a class='clas'>{{:Freight}}</a>").Add(); 
            }) 
) 

To include the template column in the exported file, you have to enable isTemplateColumnInclude parameter in the export(). To bind the template column with the mentioned anchor tag, we have used the ServerExcelQueryCellInfo event. Refer to the following code example. 

        public void ExportToExcel(string GridModel) 
        { 
            ExcelExport exp = new ExcelExport(); 
            GridProperties obj = ConvertGridObject(GridModel); 
            var DataSource = OrderRepository.GetAllRecords().ToList(); 
            obj.ServerExcelQueryCellInfo = queryCellInfo; 
            exp.Export(obj, DataSource, "Export.xlsx", ExcelVersion.Excel2010, false, true/*template columns include*/, "flat-saffron",false); 
        } 
        public void queryCellInfo(object currentCell) {  
            IRange range = (IRange)currentCell; 
            if (range.Column == 3) {// here 3 is the index of Freight/Template columns 
                range.Value = "<a class='symbol'>" + range.Value + "</a>"; 
            } 
        } 

Note: To populate the template column with the data, you have to mention the field in the columns. 

Refer to the following Help Documents. 



Regards, 
Seeni Sakthi Kumar S. 



JS Jeffrey Stone August 17, 2016 02:31 PM UTC

Seeni,

Thanks. Moving the <a> tag from the Format to the Template worked.

Is there documentation that better explains what the parameters do on ExcelExport.Export?

Regards, Jeff


SS Seeni Sakthi Kumar Seeni Raj Syncfusion Team August 18, 2016 01:04 PM UTC

Hi Jeffrey, 

Currently, we don’t have Help Document explaining Export Overload methods. So we have logged a task on it and we will add the Export Overload methods in Exporting sections of following Help Document.  


Regards, 
Seeni Sakthi Kumar S. 


Loader.
Up arrow icon