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

Exporting RadGrid directly to Excel without client side trigger

3 Answers 370 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 25 Jan 2013, 06:06 AM
I have created my RadGrid and have exporting to Excel working well when the user clicks on an export button in their browser.

I have also used the code below to make it so that the Excel file can be saved programatically without bringing up the file save dialog.  This code is also working well.

Now, what I need to do is automate and schedule this process:   the creation of the grid, the firing off the export to Excel function, and saving that exported Excel file to a directory on the web server.  In order to accomplish that, I created a windows application that would sit on the same web server and would call the web page that contains the grid.   That works fine.

The problem that I have is that I don't know how to fire the GridExporting event without using some client side interaction or client-side javascript.   I am using an HttpWebRequest in my windows application to make the web request, but HttpWebRequest doesn't support javascript, so it can't trigger the GridExporting event.

Is there anyway to call the GridExporting event from the server-side and not the client-side?   If so, during what event should I make that call from and what does that call look like?

I know that as an alternative, I could totally skip using RadGrid, and for these automated exporting calls, simply take my raw data and create an excel sheet programatically using NPOI or some other tool and save it.   The issue is that I will still be using RadGrid on the web, and I have built the web application so that the data in the grid can be customized significantly by my users (through sorting, grouping, hiding columns, etc), and I really want the programatically created Excel file to provide the same data, in as close to the same format, as if the user used the RadGrid's export to Excel function on the website.  

It seems that I am very close to making that happen, if I can just figure out how to trigger the export function on the server-side.

Thanks in advance for your help!


This is the code that I am using to save the Excel file programatically without bringing up the file save dialog.  This code is working great.
Protected Sub RadGrid1_GridExporting(sender As Object, e As Telerik.Web.UI.GridExportingArgs) Handles myGrid.GridExporting
        'This handler saves the file DIRECTLY to the server directory without showing the save as dialog
             
            path = Server.MapPath("~/MyFile.xls")
 
            Dim fs As FileStream
            fs = File.Create(path)
 
            Dim info As Byte()
            info = System.Text.Encoding.Default.GetBytes(e.ExportOutput)
            fs.Write(info, 0, info.Length)
            fs.Close()
 
            Response.Redirect(Request.Url.ToString(), False)
 
    End Sub

3 Answers, 1 is accepted

Sort by
0
Kostadin
Telerik team
answered on 28 Jan 2013, 08:59 AM
Hello Greg,

I already answered your support ticket but I will paste the solution here as well if somebody else have a similar scenario. The following CodeLibrary demonstrates another way to achieve your requirement. Basically you fetch the data from your datasource and then manually generate the desired structure using ExportInfrastructure.

All the best,
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.
0
Greg
Top achievements
Rank 1
answered on 28 Jan 2013, 04:16 PM
Thanks for the reply.

As I discussed, I would greatly prefer to generate the Excel sheet from the RadGrid (instead of using an approach like ExportingInfrasctructure) because I am allowing my users to heavily customize the the RadGrid.   So, my question again is:

Is it possible to call the GridExporting event from the server-side and not the client-side?

Thanks again for your help!
0
Daniel
Telerik team
answered on 31 Jan 2013, 02:02 PM
Hello Greg,

GridExporting event is not what creates the Excel file. It is exposed to give access to the already generated output. That said, calling the event alone will not help you in any way.
If you want to trigger the export, you just have to call the ExportToExcel method of the MasterTableView.

Kind regards,
Daniel
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
Greg
Top achievements
Rank 1
Answers by
Kostadin
Telerik team
Greg
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or