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

Excel export asp.net from a vritual perspective

6 Answers 16 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Leon
Top achievements
Rank 1
Leon asked on 16 May 2014, 01:01 PM
I have a page where I show the user 4 columns from an sql query, in a grid.
If they export , we have to include all 15 columns, which leads me to wonder if I could not just create all in the code behind, a new virtual instance of a radgrid, and populate it with my full sql query result set, and then use the exporttoexcel() method call without showing the user the second grid.

Is this possible? If so, what would be the limitations or tips to making sure this works seemless?

Thank you for you help in advance.

6 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 1
answered on 17 May 2014, 07:00 AM
Hi Leon,

You can create a RadGrid on the Export button with all the columns you need and Export that to Excel. Please take a look at the sample code snippet.

ASPX:
<asp:Button ID="btnExport" runat="server" Text="Export To Excel" OnClick="btnExport_Click" />
<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowPaging="true" AllowSorting="true">  
</telerik:RadGrid>
<asp:PlaceHolder ID="PlaceHolder1" runat="server"></asp:PlaceHolder>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT  OrderID,ShipCity FROM [Orders]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="SELECT OrderID,ShipCity,CustomerID,ShipVia FROM [Orders]"></asp:SqlDataSource>

C#:
protected void btnExport_Click(object sender, EventArgs e)
{
  RadGrid grid = new RadGrid();
  grid.ID = "RadGrid2";
  grid.DataSourceID = "SqlDataSource2";
  grid.AutoGenerateColumns = true;
  PlaceHolder1.Controls.Add(grid);
  grid.MasterTableView.ExportToExcel();
}

Thanks,
Princy
0
Leon
Top achievements
Rank 1
answered on 19 May 2014, 01:22 PM
I don't know the SQL query or bind the datasource in advance, will this be a problem as I see in your example that the datasource actually also is declared on the .aspx side of things...

My version would have the sqldatasrouce declared and populated one line above the assignment to the grid
>grid.DataSourceID = "SqlDataSource2";

Thanks in advance for the example though...
0
Leon
Top achievements
Rank 1
answered on 19 May 2014, 06:41 PM
Let me just rephrase my last statement...

I would use a dynamically generated datasource with filters based on the users selection all in code behind, and I think I would normally replace the datasourceId line with the following

grid.DataSource = GetDataSource(context); <- context being entity data model

As right now my first grid is populated with the databading being set once the user clicks a button as follows...
ReBindResult(); .... which in turn calls the OnSearchResultNeedDataSource associated to my grid

So as you see, I am now creating a new grid, using the same filters, needing to populate the entity model in code behind and call the export to excel without posting data to the user....is this still feasible the way I am thinking it?
0
Leon
Top achievements
Rank 1
answered on 20 May 2014, 04:36 PM
I was able to define a separate page as I was not able to get the export to work on the same current page, something to do with ajax, panels and windowmanagers all being too much for the logic on just one page, so i used your example and got it to work on a separate page, using a sqldatasource, but setting the command dynamically in code behind.... however now what I am getting is a "hang" when my first page button calls the second page , in the pageload i do the export, and then i was going to redirect it back to the main page of the website (as the export is a final setp in the process).

I get the RadAjaxLoadingPanel stuck (think its from the first page) and hangs... but what I dont get is that the code from the second page was called, and if I take out the exporttoexcel() call, it works as it is supposed to, yet with the code there, it calls, i get to export the file, but after the export if done it still hangs there ....

Just wondering how to make this work, almost at the end now, would be nice to actually get this working properly.

thanks a million in advance guys...
0
Kostadin
Telerik team
answered on 21 May 2014, 08:11 AM
Hello Leon,

A possible solution is to manually generate the exported file directly from the DataSource by using the ExportInfrastructure. Please check out the following code library which demonstrates how to achieve that.

Regards,
Kostadin
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Leon
Top achievements
Rank 1
answered on 21 May 2014, 01:51 PM
Thank you Kostadin for the example.
What I ended up doing as I was limited with my time is a slightly different approach.
I ended up adding a hyperlink that called another page in a different tab to avoid overlapping ajax issues, then (in page_load event) where upon opening, just created the dynamic grid data source, added it to the page's panel and then used the exporttoexcel command, then closed the page/tab. I was seeing too much issues with other controls on same page within radwindowmanagers and radajaxpanels etc... so I kept it simple, redirect to a simple page that does just what I need it to do, used the dynamic code to generate the content in code behind, and now it works.

I could include the code for others if they want, but it really is almost the same as the examples you have on your site, just redirect to an extra page for the download that way your first page does not have to deal with it....

Thanks for the example though, it gives me even more ideas, I appreciate the help. thanks
Tags
Grid
Asked by
Leon
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Leon
Top achievements
Rank 1
Kostadin
Telerik team
Share this question
or