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.
Unfortunately, activation email could not send to your email. Please try again.

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

Thread ID:

Created:

Updated:

Platform:

Replies:

125412 Aug 15,2016 09:44 PM Aug 18,2016 09:04 AM ASP.NET MVC 5
loading
Tags: Grid
Jeffrey Stone
Asked On August 15, 2016 09:44 PM

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





Mani Sankar Durai [Syncfusion]
Replied On August 16, 2016 09:31 AM

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. 


Jeffrey Stone
Replied On August 16, 2016 11:06 AM

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



Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On August 17, 2016 07:29 AM

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. 


Jeffrey Stone
Replied On August 17, 2016 10:31 AM

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

Seeni Sakthi Kumar Seeni Raj [Syncfusion]
Replied On August 18, 2016 09:04 AM

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. 


CONFIRMATION

This post will be permanently deleted. Are you sure you want to continue?

Sorry, An error occured while processing your request. Please try again later.

You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.

;