This is a migrated thread and some comments may be shown as answers.

RadGrid ExportExcel Problem.

1 Answer 156 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jignesh
Top achievements
Rank 1
Jignesh asked on 08 Jun 2010, 03:52 PM
Hi I have problem in exporting to excel from the grid. It throws exception if I set
<Excel Format = "excelML">
for <Excel Format="html"> its working fine.
Here is my grid settings.

 

 

 

 

<telerik:RadGrid ID="grdCheckData" runat="server" AllowPaging="True"  AllowSorting ="true"  GridLines="both"  OnPageIndexChanged="grdCheckData_PageIndexChanged" OnPageSizeChanged="grdCheckData_PageSizeChanged"    
    OnSortCommand="grdCheckData_SortCommand"  ShowGroupPanel="true"  ><%--OnExcelExportCellFormatting="grdCheckData_ExcelExportCellFormatting" OnExcelMLExportRowCreated="grdCheckData_ExcelMLExportRowCreated" OnExcelMLExportStylesCreated="grdCheckData_ExcelMLExportStylesCreated" --%> 
     
         <ClientSettings  AllowColumnsReorder ="true"  AllowDragToGroup="true"  ReorderColumnsOnClient="true"  Scrolling-AllowScroll="true"   > 
             <Resizing AllowRowResize="True" EnableRealTimeResize="True" ResizeGridOnColumnResize="True" 
                            AllowColumnResize="True"></Resizing> 
                  
            </ClientSettings> 
            <GroupingSettings ShowUnGroupButton="true" UnGroupTooltip ="Drag the field to group by." /> 
            <SortingSettings SortedBackColor="Azure" EnableSkinSortStyles="false" /> 
            <ExportSettings FileName ="CheckInfo" OpenInNewWindow="true"   IgnorePaging ="true" ExportOnlyData="true"  > 
<Excel Format="excelML"   /> 
<Pdf AllowPrinting="true" /> 
</ExportSettings> 
        <MasterTableView AutoGenerateColumns ="false"  > 
         
            <RowIndicatorColumn> 
                <HeaderStyle Width="20px" /> 
            </RowIndicatorColumn> 
            <ExpandCollapseColumn> 
                <HeaderStyle Width="20px" /> 
            </ExpandCollapseColumn > 
            <Columns  > 
            <telerik:GridBoundColumn DataField ="BANK"  UniqueName ="BANK" SortExpression="BANK" ReadOnly ="true" HeaderText ="Bank" > <ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="BANKNO" UniqueName ="BANKNO" SortExpression ="BANKNO" ReadOnly ="true" HeaderText ="Bank ID" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="PMSID" UniqueName ="PMSID" SortExpression ="PMSID" ReadOnly ="true" HeaderText ="PMS ID" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="SSN" UniqueName ="SSN" ReadOnly ="true" SortExpression ="SSN" HeaderText ="SSN" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="FNAME" UniqueName ="FNAME" ReadOnly ="true" SortExpression ="FNAME" HeaderText ="First Name" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="LNAME" UniqueName ="LNAME" ReadOnly ="true" SortExpression ="LNAME" HeaderText ="Last Name" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="LET" UniqueName ="LET" ReadOnly ="true" HeaderText ="Let" SortExpression ="LET" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="CHECKNUM" UniqueName ="CHECKNUM" ReadOnly ="true" SortExpression ="CHECKNUM" HeaderText ="Check No." ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="CHECKDT" UniqueName ="CHECKDT" ReadOnly ="true" SortExpression ="CHECKDT" HeaderText ="Check Date" DataType = "System.DateTime" DataFormatString="{0:MM/dd/yyyy}"  HtmlEncode="false" ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="NETPAY" UniqueName ="NETPAY" DataFormatString="{0:$###,###.##}" ReadOnly ="true" SortExpression ="NETPAY" HeaderText ="Net Amt." ><ItemStyle Width ="100" /></telerik:GridBoundColumn> 
            <telerik:GridBoundColumn DataField ="DISPDT" UniqueName ="DISPDT" ReadOnly ="true" SortExpression ="DISPDT"  HeaderText ="Disp. Date" DataType = "System.DateTime" DataFormatString="{0:MM/dd/yyyy}"  HtmlEncode="false" > 
            <ItemStyle Width ="100" /> 
            </telerik:GridBoundColumn> 
              
            </Columns> 
        </MasterTableView> 
      
    </telerik:RadGrid> 

Here is my code for export.

protected void btnExportExl_Click(object sender, EventArgs e)  
        {  
            try  
            {  
                if ((!IsPostBack) || (!IsValid))  
                    return;  
 
                RadGrid grdCheckData = (RadGrid)dataPanel.FindItemByValue("CheckData").FindControl("grdCheckData");  
                  
                grdCheckData.MasterTableView.ExportToExcel();  
            }  
            catch (Exception ex)  
            {  
                throw;  
            }  
        }  
 
        protected void grdCheckData_ExcelExportCellFormatting(object source, ExcelExportCellFormattingEventArgs e)  
        {  
              
        }  
 
        protected void grdCheckData_ExcelMLExportStylesCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLStyleCreatedArgs e)  
        {  
            foreach (Telerik.Web.UI.GridExcelBuilder.StyleElement style in e.Styles)  
            {  
                if (style.Id == "headerStyle")  
                {  
                    style.FontStyle.Bold = true;  
                    style.FontStyle.Color = System.Drawing.Color.Brown;  
                    style.InteriorStyle.Color = System.Drawing.Color.Wheat;  
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;  
                }  
                else if (style.Id == "itemStyle")  
                {  
                    style.NumberFormat.FormatType = Telerik.Web.UI.GridExcelBuilder.NumberFormatType.Currency;  
                    style.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.HorizontalAlignmentType.Left;  
                    style.InteriorStyle.Color = System.Drawing.Color.WhiteSmoke;  
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;  
                }  
                else if (style.Id == "alternatingItemStyle")  
                {  
                    style.NumberFormat.FormatType = Telerik.Web.UI.GridExcelBuilder.NumberFormatType.Currency;  
                    style.AlignmentElement.HorizontalAlignment = Telerik.Web.UI.GridExcelBuilder.HorizontalAlignmentType.Left;  
                    style.InteriorStyle.Color = System.Drawing.Color.LightGray;  
                    style.InteriorStyle.Pattern = Telerik.Web.UI.GridExcelBuilder.InteriorPatternType.Solid;  
                }  
 
 
            }  
        }  
 
        protected void grdCheckData_ExcelMLExportRowCreated(object source, Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowCreatedArgs e)  
        {  
            if (e.RowType == Telerik.Web.UI.GridExcelBuilder.GridExportExcelMLRowType.HeaderRow)  
            {  
                Telerik.Web.UI.GridExcelBuilder.RowElement row = new Telerik.Web.UI.GridExcelBuilder.RowElement();  
                Telerik.Web.UI.GridExcelBuilder.CellElement cell = new Telerik.Web.UI.GridExcelBuilder.CellElement();  
                cell.Data.DataItem = "NYC Department Of Education";  
                row.Cells.Add(cell);  
                e.Worksheet.Table.Rows.Insert(0, row);  
            }    
 
        }  
 
        

And this is the Error I m getting after execution of btnExport_Click event.
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:   
 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.    
 
Stack Trace:   
 
 
[NullReferenceException: Object reference not set to an instance of an object.]  
   Telerik.Web.UI.Grid.Export.TableViewExporter.ExcelExportRenderForm(HtmlTextWriter nullWriter, Control form) +864  
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256  
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19  
   System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) +8676409  
   System.Web.UI.HtmlControls.HtmlContainerControl.Render(HtmlTextWriter writer) +32  
   System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) +51  
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27  
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99  
   System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) +40  
   Telerik.Web.UI.Grid.Export.TableViewExporter.ExcelExportRenderPage(HtmlTextWriter nullWriter, Control page) +93  
   System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) +256  
   System.Web.UI.Control.RenderChildren(HtmlTextWriter writer) +19  
   System.Web.UI.Page.Render(HtmlTextWriter writer) +29  
   System.Web.UI.Control.RenderControlInternal(HtmlTextWriter writer, ControlAdapter adapter) +27  
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer, ControlAdapter adapter) +99  
   System.Web.UI.Control.RenderControl(HtmlTextWriter writer) +25  
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1266  
 
   
 
 
--------------------------------------------------------------------------------  
Version Information: Microsoft .NET Framework Version:2.0.50727.3082; ASP.NET Version:2.0.50727.3082  

1 Answer, 1 is accepted

Sort by
0
Accepted
Daniel
Telerik team
answered on 08 Jun 2010, 04:28 PM
Hello Jignesh,

Please use advanced data-binding to avoid this problem. I recommend that you examine the following links:
Advanced Data-binding
ExcelML export

Regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
Grid
Asked by
Jignesh
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Share this question
or