Showing posts with label spreadsheet. Show all posts
Showing posts with label spreadsheet. Show all posts

Monday, March 26, 2012

Exporting GridView Contents to Excel Spreadsheet

Hello,

I currently have a report that takes about 3 minutes to process before outputting around 1500 lines to a non-paginated gridview. I wanted to add an UpdateProgress with an animated GIF to let the user know that it is processing, and add a cancel button using the new functionality from AJAX RC.

I cannot completely successfully implement my idea. If I leave the gridview in the UpdatePanel, it will update correctly and display on the page, but when I click the Export to Excel button, it causes some Response.Writes to happen, and it throws a Sys.Webforms.parsererror (which I have looked at some discussions of). The second option is to leave the gridview out of the update panel, which allows the data to export perfectly into Excel, but the page doesn't update the gridview (since it is not in the UpdatePanel and is not part of the Async Postback).

Preferably, I'd like to find a way to keep the Gridview in the UpdatePanel and get the Export button to work.

Here is the code for my export button:

1protected void btnExportToExcel_Click1(object sender, EventArgs e)2 {3 Response.Clear();4 Response.AddHeader("content-disposition","attachment;filename=HREN_Form_Not_Completed.xls");5 Response.Charset ="";6// If you want the option to open the Excel file without saving than7 // comment out the line below8 // Response.Cache.SetCacheability(HttpCacheability.NoCache);9 Response.ContentType ="application/vnd.xls";10 System.IO.StringWriter stringWrite =new System.IO.StringWriter();11 System.Web.UI.HtmlTextWriter htmlWrite =new HtmlTextWriter(stringWrite);12 gvReport.RenderControl(htmlWrite);13//Response.Write(cboAssociate.SelectedItem.Text + "_" + cboMonth.SelectedItem.Text + "_" + cboYear.SelectedItem.Text);14 Response.Write(stringWrite.ToString());15 Response.End();1617 }

Any ideas? Thanks!

Here are some sample codes to export to Excel based on Ajax RC 1.0 for your reference.
<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<asp:Panel ID="pnl2Export" runat="server">
<asp:Table ID="Table1" runat="server" Width="187px">
<asp:TableRow runat="server">
<asp:TableCell runat="server">This will Print if this is successful!</asp:TableCell>
<asp:TableCell runat="server">Export to Excel in a asp:UpdatePanel</asp:TableCell>
</asp:TableRow>
</asp:Table>
</asp:Panel>
<asp:Button ID="Postback" runat="server" Text="Export Control (panel)" OnClick="Postback_Click" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Postback" />
</Triggers>
</asp:UpdatePanel>

Behind Codes:

protected void Postback_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=ExcelReport.xls");
Response.Charset = "";
//Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
pnl2Export.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}

Wish the above can help you.

Export to Excel within a AJAX Updatepanel

Hi, I'm getting an "pagerequestmanagerparsererrorexception" error when trying to export a gridview to an excel spreadsheet within an updatepanel.

here is the code below ;

Protected Sub Export2Excel() Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=TestExcel.xls") Response.Charset = "" 'If you want the option to open the Excel file without saving than 'comment out the line below Response.Cache.SetCacheability(HttpCacheability.NoCache) Response.ContentType = "application/vnd.xls" Dim stringWrite As System.IO.StringWriter = New System.IO.StringWriter() Dim htmlWrite As System.Web.UI.HtmlTextWriter = New HtmlTextWriter(stringWrite) Try Results_GridView.RenderControl(htmlWrite) Response.Write(stringWrite.ToString) Catch ex As Exception End Try Response.End() End Sub
 Any help would be greatly appreciate..
 Thanks,
Clint 

You need to open up the excel in a pop up window or in an iframe. You can not send that content down through the Ajax Request and expect it to download on the users computer.

Eric


Hi Clint,

I think the simplest way is do not put the button inside any update panel.

Hope this helps.


Hi,

I am using a link button within the update panel, this is when I am getting the error.

Thanks


i could make it, adding an

<Triggers>
<asp:PostBackTrigger ControlID="ExportingButton" />
</Triggers>

to the updatepanel... hope this helps!!!

by!


Hi, the problem it's that when you use ajax you can't continue using theresponse.write,because the big idea of Ajax is not write all the page, only someparts, well the solution it's very easy, add an page and send StringWrite to a session variable, and add an Response.Redirect to the new page without ajax and in the load of the new page write your Response.write, and that's all, that will only open a windows in the client side asking if want to save or open the file, hope this be usefull fou U.


I've handled this in this post:

http://forums.asp.net/t/1047851.aspx

Hope it helps!

Bebandit