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

Save directly to file by export to excel feature of RadGrid

1 Answer 162 Views
Grid
This is a migrated thread and some comments may be shown as answers.
RBarnes
Top achievements
Rank 1
RBarnes asked on 18 Oct 2012, 07:39 PM


I posted this originally on this thread Save to File but haven't gotten any replies for a few days, so maybe that thread isn't being looked at, so I'm reposting here.

I'm using RadControls for ASP.NET AJAX Q2 2012 SP2

I'm using the described method to export to excel file, and it seems to work if I'm exporting a single grid. However, In my case I have three grids and I need to export all three to seperate excel files, then continue to do some additional processing after the exports are completed.

When I attempt to export mutliple excel files one after the other, none of the exports work. NO errors, NO nothing, see code excerpts below.

In addition, is there any way to suppress the open/save dialog when writing the exported file to disk, other than
Response.Redirect(Request.Url.ToString())

The issue seems to be with exporting multiple grids back to back.

I've also tried firing the Export Command from codebehind, the grids ItemCommand runs, but GridExporting doesn't.
Dim GCI As Telerik.Web.UI.GridCommandItem = CType(Me.rgSummary.MasterTableView.GetItems(Telerik.Web.UI.GridItemType.CommandItem)(0), Telerik.Web.UI.GridCommandItem)
  
GCI.FireCommandEvent("EXPORTTOEXCEL", String.Empty)


Any help would be appreciated.


Protected Sub btnExport_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExport.Click
Me.rgSummary.Rebind()
Me.rgSummary.ExportSettings.FileName = "Fantastic_Invoice_Summary_" + Format(Now, "MM_dd_yyyy_hh_mm_tt")
Me.rgSummary.ExportSettings.OpenInNewWindow = False
Me.rgSummary.MasterTableView.ExportToExcel()
'If I exit sub here, I'll get the summary excel file.
Me.rgDetail.Rebind()
Me.rgDetail.ExportSettings.FileName = "Fantastic_Invoice_Detail_" + Format(Now, "MM_dd_yyyy_hh_mm_tt")
Me.rgDetail.ExportSettings.OpenInNewWindow = False
Me.rgDetail.MasterTableView.ExportToExcel()
'I get neither file if I don't exit after the summary export
Me.rgCommission.Rebind()
If Me.rgCommission.Items.Count <> 0 Then
Me.rgCommission.ExportSettings.FileName = "Fantastic_Invoice_Commission_" + Format(Now, "MM_dd_yyyy_hh_mm_tt")
Me.rgCommission.ExportSettings.OpenInNewWindow = False
Me.rgCommission.MasterTableView.ExportToExcel()
End If
'Additional processing below
end sub
Private Sub rgSummary_GridExporting(sender As Object, e As Telerik.Web.UI.GridExportingArgs) Handles rgSummary.GridExporting
Dim fs As System.IO.FileStream
Dim FileName As String = Server.MapPath("~/Temp/") + Me.rgSummary.ExportSettings.FileName + "." + Me.rgSummary.ExportSettings.Excel.FileExtension
fs = System.IO.File.Create(FileName)
'Dim output As Byte() = Encoding.GetEncoding(1252).GetBytes(e.ExportOutput)
Dim output As Byte() = System.Text.Encoding.Default.GetBytes(e.ExportOutput)
fs.Write(output, 0, output.Length)
fs.Close()
End Sub

1 Answer, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 23 Oct 2012, 01:13 PM
Hi Roger,

Please note that this scenario could not be achieved out-of-the-box. The reason is that exporting is performed too late in the page lifecycle (on PreRender and Render), and only one export at a time could be executed, because the exported file is streamed to the response.
A possible custom approach for achieving the required functionality is when you first load the page, to create a session where you could keep information about the grids (for example you could keep an array with the RadGrid's IDs). When you call GridExporting after saving the file to the server you could redirect and do the same with the second and the third grid. Here it is, step by step:
  • Export the first file
  • Handle the GridExporting event and get the generated output
  • Save the aforementioned output to a file on the server
  • Refresh the page (Response.Redirect)
  • Export the second file (call RadGrid1.MasterTableView.ExportToExcel programmatically)
  • Handle GridExporting and get the output
  • Save the second file
  • Refresh the page again
  • Export the third file
  • Handle GridExporting and get the output
  • Save the second file
  • Refresh again

I hope this information helps.

Regards,
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
RBarnes
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Share this question
or