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

radgrid export to excel not working

10 Answers 1870 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Jeff
Top achievements
Rank 1
Jeff asked on 22 Jan 2014, 01:53 PM
Here is my grid:

                <asp:Button ID="btnExcel" runat="server" CssClass="inpButton" Text="Export to Excel" OnClick="btnExcel_Click" />
                <telerik:RadGrid ID="grdInvoices" AutoGenerateColumns="false" AllowSorting="false" AllowMultiRowSelection="false" runat="server">
                    <MasterTableView>
                        <Columns>
                            <telerik:GridBoundColumn HeaderText="Invoice Number" UniqueName="InvNum" DataField="invoiceNumber" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Note Code" UniqueName="Note" DataField="noteCode" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="PO Number" UniqueName="po" DataField="poNumber" HeaderStyle-Width="30%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Invoice Date" UniqueName="invDate" DataField="invoiceDate" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Original Invoice Balance" UniqueName="OrigBal" DataField="origBalance" HeaderStyle-Width="15%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Invoice Payments" UniqueName="InvPay" DataField="paymentAmount" HeaderStyle-Width="10%"></telerik:GridBoundColumn>
                            <telerik:GridBoundColumn HeaderText="Present Invoice Balance" UniqueName="InvBal" DataField="presentBalance" HeaderStyle-Width="15%"></telerik:GridBoundColumn>
                        </Columns>
                    </MasterTableView>
                    <ClientSettings EnableRowHoverStyle="true">
                        <Selecting AllowRowSelect="True"></Selecting>
                    </ClientSettings>
                </telerik:RadGrid>


And here is the button click logic:
    protected void btnExcel_Click(object sender, EventArgs e)
    {
        try
        {
            grdInvoices.ExportSettings.ExportOnlyData = true;
            grdInvoices.ExportSettings.IgnorePaging = true;
            grdInvoices.ExportSettings.OpenInNewWindow = true;
            grdInvoices.ExportSettings.FileName = @"C:\jasInv.xlxs";
            grdInvoices.MasterTableView.ExportToExcel();
        }
        catch (Exception ex)
        {
            txtError.Text = ex.Message;
        }
    }

It does nothing.  The file isn't created, no errors, nothing.

10 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 23 Jan 2014, 03:26 AM
Hi Jeff,

The exporting feature works only with regular postbacks. This means, that the asynchronous postback should be canceled when performing an export. So if you have ajaxified your RadGrid make sure you explicitly disable its ajax mode to export with regular postback. Please look into this documentation for more information on Export from Ajaxified Grid

JavaScript:
function onRequestStart(sender, args) {
  if (args.get_eventTarget().indexOf("btnExcel") >= 0)
    args.set_enableAjax(false);
}

Thanks,
Princy


0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 05 Jun 2015, 06:14 PM
What about for the built-in CommandItem buttons that do this?
0
Eyup
Telerik team
answered on 10 Jun 2015, 01:19 PM
Hi Allen,

The IDs of the automatically generated Export buttons are provided in the documentation link from the previous post:
  • ExportToExcelButton
  • ExportToWordButton
  • ExportToPdfButton
  • ExportToCsvButton

Hope this helps.

Regards,
Eyup
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Digger
Top achievements
Rank 1
answered on 24 Jul 2015, 02:07 PM

Not understanding the automatically generated Export buttons, and how i tie the onRequestStart function to be called.

Have multiple RadGrids in a RadMultiPageView.  (No asp panel wrapping grids).  I created the script:

function onRequestStart(sender, args) {
                if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0)
                    args.set_enableAjax(false);
                if (args.get_eventTarget().indexOf("ExportToWordButton") >= 0)
                    args.set_enableAjax(false);
                if (args.get_eventTarget().indexOf("ExportToPdfButton") >= 0)
                    args.set_enableAjax(false);
                if (args.get_eventTarget().indexOf("ExportToCsvButton") >= 0)
                    args.set_enableAjax(false);

}

Who needs to call this function?  The Grid? the Multi page?  What control do i need to set the OnRequestStart to be called?  Is this call only available for asp panels?  Do i have to create my own buttons and not use the built in Command Items?

Thanks

0
Digger
Top achievements
Rank 1
answered on 24 Jul 2015, 02:37 PM

Ok, Found the OnRequestStart function in the AjaxManager.  Did not work with the ClientEvents-OnRequestStart (All my Multipage Views stopped working).  When i moved the command to <ClientEvents OnRequestStart="myscript" /> it worked.

 My fustration here is that your examples all seem to work with user created buttons.   I could not find any examples with your built-in CommandItem functions for ​export.   Nor could i find the explicit call to OnRequestStart  in the AjaxManager in your examples.

 This is your built-in function - Give explicit full examples of your default built-in functions.;   I find this throughout all your controls, in that you gloss over your built in functions and how to implement, and give many examples of custom controls.   Wasted many hours thinking i had to build this custom control to make what you have built-in already work because you just gloss over the built in control functions....

 

0
Kostadin
Telerik team
answered on 29 Jul 2015, 07:18 AM
Hello Digger,

Note that before introducing the XLSX and DOXS export format we were using the built-in command item buttons to export the grid in our example. Nevertheless in order to demonstrates the newly added formats our designer decided that it will be easier for customer to try them by using an external buttons. This way there is a different button for each export format and you can test them without any additional modifying.
However thank you for your feedback. I will discuss your suggestion with our designers and we will change the layout of the page and we will include the built-in buttons in the demos.

Regards,
Kostadin
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Paul
Top achievements
Rank 1
answered on 11 Jan 2016, 05:04 PM

Would have to strongly agree with Digger on his point regarding it being completely confusing.

On the surface it seems like you would do this in the Ajax code, but this doesn't work for me:

<telerik:RadAjaxManager ID="RadAjaxManager1" runat="server" ClientEvents-OnRequestStart="onRequestStart"</telerik:RadAjaxManager>

In the javascript region ... 

function onRequestStart(sender, args) {
            if (args.get_eventTarget().indexOf("ExportToExcelButton") >= 0)
                args.set_enableAjax(false);
            if (args.get_eventTarget().indexOf("ExportToPdfButtonn") >= 0)
                args.set_enableAjax(false);
            if (args.get_eventTarget().indexOf("ExportToCsvButton") >= 0)
                args.set_enableAjax(false);
}

 

0
Kostadin
Telerik team
answered on 14 Jan 2016, 12:53 PM
Hi Paul,

Generally speaking the approach which stops the ajax request could be use for any controls which perform such request and not only the buttons which export the grid. When onRequestStart is fired you can get the target element which perform the request and you can check if this element is the button which fired the export command. This way you can manually disable the ajax and perform a full postback which is required when export the grid.
You can see this approach in action in the following live example. I would appreciate if you can debug the client code and check what is the value when you call args.get_eventTarget(). Additionally you can check the value when you call args.get_eventArgument() as well.

Regards,
Kostadin
Telerik
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 Feedback Portal and vote to affect the priority of the items
0
Paul
Top achievements
Rank 1
answered on 18 Jan 2016, 09:42 AM

I'm still in string agreement with Digger about the confusing. All that we want to do is use the standard button as its designed to work with Ajax. Radgrid should have better programming to deal with supporting Ajax and exports together.

In the end, I just added a client script in the Content section to force the 'postback'. I had to catch a few postbacks for other features and cancel them, but its simple and works. 

<script type="text/javascript"> 

  function OnClientItemClicked() { __doPostBack(); }

</script>

0
Kostadin
Telerik team
answered on 21 Jan 2016, 07:26 AM
Hello Paul,

With our latest official release (Q1 2016) there is not need to cancel the ajax if you are using the built-in buttons. Please check out the following help article for more information.

Regards,
Kostadin
Telerik
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 Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Jeff
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Allen
Top achievements
Rank 2
Iron
Veteran
Eyup
Telerik team
Digger
Top achievements
Rank 1
Kostadin
Telerik team
Paul
Top achievements
Rank 1
Share this question
or