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

Export to Excel not working

3 Answers 356 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Chris
Top achievements
Rank 1
Chris asked on 10 May 2013, 09:48 AM
Hi,

Please can I have some help as so far every thing I have tried with the grid has not worked as expected and I have now logged 3 posts where functions that should work just don't seem to.  Perhaps it is the way in which I am populating my grid?  All columns and data are created dynamically in code-behind as I do not know what will be displayed.  

Essentially I have just added export to excel via the CommandItem as per

<MasterTableView Height="30px" CommandItemDisplay="Top">
<CommandItemSettings ShowExportToExcelButton="true" ShowAddNewRecordButton="false" ShowExportToPdfButton="true" />
<Columns>
<telerik:GridDragDropColumn HeaderStyle-Width="18px" Visible="false" />
</Columns>
</MasterTableView>

From the demo project I have also implemented the events

OnItemDataBound="gridEvents_ItemDataBound" 
OnItemCommand="gridEvents_ItemCommand"
OnBiffExporting="gridEvents_ExportToExcel"

Although when looking at the code in the sample this actually does nothing in most cases.  I have also added the 

using eis = Telerik.Web.UI.ExportInfrastructure;

line to the top of my file - this is easy to miss as it is not mentioned in the sample.  I see the buttons (although the refresh button is disabled?) but when clicking export to excel/pdf nothing happens, no errors no calling of the above events nothing.

Can someone (maybe from Teleik?) let me know what the common factor is here - why does export to excel / row drag and drop not work for me?  Started to get frustrated here as I was trying to move away from Infragistics as their grid was somewhat complex/heavy but at least I could get it to work!

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 10 May 2013, 01:21 PM
Hi,

Please take a look into the following code snippet I tried.

ASPX:
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true"
        OnItemDataBound="RadGrid1_ItemDataBound" OnItemCommand="RadGrid1_ItemCommand"
        AutoGenerateColumns="false" OnBiffExporting="RadGrid1_BiffExporting">
        <MasterTableView CommandItemDisplay="Top">
            <CommandItemSettings ShowExportToExcelButton="true"></CommandItemSettings>
            <Columns>
                <telerik:GridBoundColumn DataField="EmployeeID" HeaderText="Employee ID" DataFormatString="{0:000}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="LastName" HeaderText="Last Name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="FirstName" HeaderText="First Name">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="BirthDate" HeaderText="Birth Date" DataFormatString="{0:MM-dd-yy}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="HireDate" HeaderText="Hire Date" DataFormatString="{0:MM/dd/yyyy}">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Address" HeaderText="Address">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="City" HeaderText="City">
                </telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Country" HeaderText="Country">
                </telerik:GridBoundColumn>
            </Columns>
        </MasterTableView>
        <ExportSettings SuppressColumnDataFormatStrings="false">
            <Excel Format="Biff"></Excel>
        </ExportSettings>
    </telerik:RadGrid>
    <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        SelectCommand="SELECT * FROM [Employees]" runat="server"></asp:SqlDataSource>

C#:
bool isExporting = false;
  
        protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e)
        {
            if (isExporting)
            {
                if (e.Item.ItemType == GridItemType.AlternatingItem)
                {
                    e.Item.BackColor = System.Drawing.Color.LightGray;
                }
                else if (e.Item.ItemType == GridItemType.Header)
                {
                    e.Item.BackColor = System.Drawing.Color.DarkSlateGray;
                    e.Item.ForeColor = System.Drawing.Color.White;
                    e.Item.Font.Bold = true;
                    e.Item.Height = Unit.Point(20);
                    e.Item.Font.Size = 11;
                }
                else
                {
                    e.Item.BackColor = System.Drawing.Color.LightSlateGray;
                }
            }
        }
  
        protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
        {
            if (e.CommandName == RadGrid.ExportToExcelCommandName)
            {
                foreach (GridColumn col in RadGrid1.MasterTableView.AutoGeneratedColumns)
                {
                    if (col.UniqueName == "ProductName" || col.UniqueName == "QuantityPerUnit")
                    {
                        col.HeaderStyle.Width = Unit.Point(150);
                    }
                    else
                    {
                        col.HeaderStyle.Width = Unit.Point(75);
                    }
  
                }
  
                RadGrid1.ExportSettings.IgnorePaging = CheckBox1.Checked;
  
                if (CheckBox3.Checked)
                {
                    isExporting = true;
  
                    if (!RadGrid1.ExportSettings.IgnorePaging)
                    {
                        RadGrid1.Rebind();
                    }
                }
            }
        }
  
        protected void RadGrid1_BiffExporting(object sender, GridBiffExportingEventArgs e)
        {
            if (CheckBox2.Checked)
            {
                eis.Table newWorksheet = new eis.Table("My New Worksheet");
  
                eis.Cell headerCell = newWorksheet.Cells[1, 1];
                headerCell.Value = "ReportData";
                headerCell.Style.BorderBottomColor = System.Drawing.Color.Black;
                headerCell.Style.BorderBottomStyle = BorderStyle.Double;
                headerCell.Style.Font.Bold = true;
                headerCell.Colspan = 3;
  
                newWorksheet.Cells[1, 2].Value = "Users";
                newWorksheet.Cells[2, 2].Value = 220;
                newWorksheet.Cells[3, 2].Value = "Verified";
                newWorksheet.Cells[3, 2].Style.ForeColor = System.Drawing.Color.Green;
  
                newWorksheet.Cells[1, 3].Value = "Guests";
                newWorksheet.Cells[2, 3].Value = 64;
                newWorksheet.Cells[3, 3].Value = "Verified";
                newWorksheet.Cells[3, 3].Style.ForeColor = System.Drawing.Color.Green;
  
                newWorksheet.Cells[1, 4].Value = "Other";
                newWorksheet.Cells[2, 4].Value = 11;
                newWorksheet.Cells[3, 4].Value = "Pending";
                newWorksheet.Cells[3, 4].Style.ForeColor = System.Drawing.Color.Orange;
  
                e.ExportStructure.Tables.Add(newWorksheet);
            }
        }
    }

Hope this helps.

Thanks,
Princy.
0
Chris
Top achievements
Rank 1
answered on 10 May 2013, 01:30 PM
Hi,

Thanks for that but as I mentioned I started with the (working) sample from Telerik but I have to create my columns and data server side so cannot add the columns to the ASPX directly.  This is when it all stopped working.  My data is displayed correctly however neither export or drag and drop work and there are no clues given as to why.  I'd like to get this sorted but have reverted to Infragistics at the moment as it's just taking too much time sorting these issues and I have no confidence that i won't keep hitting more of the same.

Perhaps it is down to the dynamic way in which I add the columns to the gird I don't know :-(
0
Kostadin
Telerik team
answered on 15 May 2013, 11:11 AM
Hello Chris,

Could you please elaborate a little bit more on your requirement? Sending us your code declaration and the related code behind will help us to pinpoint the reason for that behavior. Meanwhile I would suggest to review the following help article which describes how to programatically create a RadGrid. As far as I understand when you click on one of the exporting buttons nothing happens. Basically this is a common behavior when the grid is ajaxified. In the following help topic could be found several ways to disable the ajax before exporting the grid.

All the best,
Kostadin
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now.
Tags
Grid
Asked by
Chris
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Chris
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or