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

export grid to word/excel server side not working

3 Answers 184 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Maya
Top achievements
Rank 1
Maya asked on 10 Apr 2012, 10:15 AM
Hi,
 im using asp .net ajax.
im having a problem while tring to export my grid to word/excel.
i used your demo (export on server side). the senario is the following:

1. i have a "command templat" in my grid, that there i put rad tool bad, and add a button of my own, of "Export to Execl", with commandName="MyExportExcel"
2. when the button is pressed  it goes to the "OnButtomClick" method of the radToolBar on the server.
3. there i compare - if this is the commant of "MyExportExcel   
 MyGrid.ExportSettings.ExportOnlyData = true; 
MyGrid.ExportSettings.IgnorePaging= true;  
MyGrid.ExportSettings.OpenInNewWindow= true;   
MyGrid.MasterTableView.ExportToExcel();

after it, the client is still stucked on the loading panel (its like continue thinking and loading) and not stoping... 
so the export not working...

how can i fix it?

and another question - if it wont work it there any way to get the byte[] of the export file, and ill make sure to bring the client a download link with the byte[] data?


 

3 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 10 Apr 2012, 10:38 AM
Hello Maya,

I have tried similar scenario and is working fine at my end. I guess you are ajaxifying the grid. If so please make sure you are disabling the ajax and then export. Here is the sample code.
aspx:
<telerik:RadAjaxLoadingPanel ID="RadAjaxLoadingPanel1" runat="server" MinDisplayTime="1000">
</telerik:RadAjaxLoadingPanel>
<telerik:RadAjaxManager ClientEvents-OnRequestStart="onRequestStart" ID="RadAjaxManager1" runat="server">
 <AjaxSettings>
   <telerik:AjaxSetting AjaxControlID="RadGrid1">
       <UpdatedControls>
         <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
      </UpdatedControls>
   </telerik:AjaxSetting>
 </AjaxSettings>
</telerik:RadAjaxManager>
<telerik:RadGrid  ID="RadGrid1" runat="server" AutoGenerateColumns="false" OnNeedDataSource="RadGrid1_NeedDataSource">
 <MasterTableView CommandItemDisplay="Top">
 <CommandItemTemplate>
   <telerik:RadToolBar ID="RadToolBar1" runat="server" AutoPostBack="true" OnButtonClick="RadToolBar1_ButtonClick">
       <Items>
         <telerik:RadToolBarButton Text="Export" runat="server">
         </telerik:RadToolBarButton>
     </Items>
  </telerik:RadToolBar>
  </CommandItemTemplate>
   <Columns>
    <telerik:GridBoundColumn DataField="EmployeeID" UniqueName="EmployeeID" HeaderText="EmployeeID"></telerik:GridBoundColumn>
    <telerik:GridBoundColumn DataField="FirstName" UniqueName="FirstName" HeaderText="FirstName"></telerik:GridBoundColumn>
    <telerik:GridBoundColumn DataField="LastName" UniqueName="LastName" HeaderText="LastName"></telerik:GridBoundColumn>
  </Columns>
 </MasterTableView>
</telerik:RadGrid>
C#:
protected void RadToolBar1_ButtonClick(object sender, RadToolBarEventArgs e)
    {
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.MasterTableView.ExportToExcel();
    }
JS:
<script type="text/javascript">
    function onRequestStart(sender, args) {
        if (args.get_eventTarget().indexOf("RadToolBar1") >= 0)
            args.set_enableAjax(false);
    }
</script>

Thanks,
Shinu.
0
Maya
Top achievements
Rank 1
answered on 15 Apr 2012, 09:15 AM
hi,

I tried to put like you said:

if (args.get_eventTarget().indexOf("RadToolBar1") >= 0)
            args.set_enableAjax(false);

but i didntt work with the id of the tool bar that in the command (it did thought worked with the id of the grid..)
but i did managed to do some hidden filed that only if i set it true, i set enable ajax to "false".. somthing like this:

function onRequestStart(sender, args) {
        if (IsExportToExcelOrWord() == "True")
            args.set_enableAjax(false);
    }

and it did finally react to me (i saw the little window of the download)
but then happened few other problems:

1. In the excel, i did:

MyGrid.ExportSettings.ExportOnlyData = true; 
MyGrid.ExportSettings.IgnorePaging= true;  
MyGrid.ExportSettings.OpenInNewWindow= true;   
MyGrid.MasterTableView.ExportToExcel(); 

the excel file was opened but EMPTY. (like a new excel file, with no data)

2. In the Word, id did:

 MyGrid.ExportSettings.ExportOnlyData = true; 
MyGrid.ExportSettings.IgnorePaging= true;  
MyGrid.ExportSettings.OpenInNewWindow= true;   
MyGrid.MasterTableView.ExportToWord();  

after it, happened an error on page, that only solved if i added the folowing in the server side:
(the function of the onRequestStart remained te same)

MyMultiPage.MyMainAjaxManager.EnableAjax = false;
 MyGrid.ExportSettings.ExportOnlyData = true; 
MyGrid.ExportSettings.IgnorePaging= true;  
MyGrid.ExportSettings.OpenInNewWindow= true;   
MyGrid.MasterTableView.ExportToWord();   

3. In the word:

it did (after step 2) opend and i did saw data in a table, but
is there any way to configure what column will be shown, in witch order (rtl/ltr)- and other kind of configurations?

4. After pressing on the "ExportToExcel" or "ExportToWord" buttons, every other request that do (like filtering, ordering, clear filering...)
is doing a full post back (the all page is rendering....), althogh in the end the order/filter works fine, but it soesnt need to be that way...
so i tried to do like this:

function onRequestStart(sender, args) {

// Init anyway
args.set_enableAjax(true); 
        if (IsExportToExcelOrWord() == "True")
            args.set_enableAjax(false);
    }

but it didnt work fix it...
how can i fix it?

I would like if you can answer my all 4 questions above, 
Thank you!!

0
Tsvetina
Telerik team
answered on 19 Apr 2012, 06:43 AM
Hello Maya,

It is expected to get full postbacks afterwards, as you have disabled AJAX. So, instead of doing this, confirm that you are not still making AJAX requests when trying to export. If this does not work:
if (args.get_eventTarget().indexOf("RadToolBar1") >= 0)
  args.set_enableAjax(false);

put a debugged in the OnRequestStart event handler and check what is the event target when the toolbar button is pressed and modify the above code accordingly.

As for formatting the exported files, check out the documentation on the different export formats, e.g.:
Word/Excel export (HTML-based)

Regards,
Tsvetina
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
Maya
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Maya
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or