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

Save directly to file by export to excel feature of RadGrid

16 Answers 908 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ajay Bhonsule
Top achievements
Rank 1
Ajay Bhonsule asked on 24 May 2010, 09:30 AM
Hi,

I have a webservice where i am creating the RadGrid Object dynamically. I have assigned the Dataview object as the datasource to this Grid. And now my requirement is to export this data directly to the filename which i mention in the code and it should be saved automatically i.e. i should not get the Save popup. I have tried the following code but it is not working faling in ExportToExcel saying "RadGrid must be databound before exporting telerik". Please let me know whether i would be able to save it directly to the file without rendering the grid control on the page as i have to do this in webservice method, no page is present. And why am getting the above mentioned error.

                RadGrid grid = new RadGrid(); 
                grid.ID = "radgridExportToExcel"
                grid.HeaderStyle.Font.Bold = true
                grid.DataSource = dsFileListToDelete.Tables[0].DefaultView; 
                string currentdatetime = DateTime.Now.ToString(); 
                string metadataReportName = string.Format(@"{0}\{1}_Metadata", Server.MapPath(@"~/WCCMetadataReports"), currentdatetime.Replace(" """).Replace("/""").Replace(":""")); 
                // render the DataGrid control to a file 
                grid.ExportSettings.ExportOnlyData = true
                grid.ExportSettings.IgnorePaging = true
                grid.ExportSettings.OpenInNewWindow = false
                grid.ExportSettings.FileName = metadataReportName; 
                grid.ExportSettings.Excel.Format = Telerik.Web.UI.GridExcelExportFormat.ExcelML; 
                grid.Rebind(); 
                grid.MasterTableView.ExportToExcel(); 

16 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 24 May 2010, 09:09 PM
Hello Ajay,

Please download the attached demo and let me know if you need further assistance.

Best regards,
Daniel
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Parth
Top achievements
Rank 1
answered on 22 Sep 2010, 02:03 PM
Hello,

I have the same issue. But the solution you have shown in the example is not working in my version (Ver. 2008.1.619.35).
It shows Radgrid does not have GridExporting property. Is there any other alternative to this ?

protected void Button1_Click(object sender, EventArgs e)
    {
        RadGrid RadGrid1 = new RadGrid();
        RadGrid1.ID = "RadGrid1";
        RadGrid1.GridExporting +=new OnGridExportingEventHandler(RadGrid1_GridExporting);
        RadGrid1.ExportSettings.Excel.Format = GridExcelExportFormat.ExcelML;
        RadGrid1.ExportSettings.ExportOnlyData = true;
        RadGrid1.DataSource = GetData();
        RadGrid1.DataBind();
  
        PlaceHolder1.Controls.Add(RadGrid1);
  
        RadGrid1.MasterTableView.ExportToExcel();
    }
  
    protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
    {
        string path = Server.MapPath("~/MyFile.xls");
        using (FileStream fs = File.Create(path))
        {
            Byte[] info = System.Text.Encoding.Default.GetBytes(e.ExportOutput);
            fs.Write(info, 0, info.Length);
        }
        Response.Redirect(Request.Url.ToString());
    }
0
Daniel
Telerik team
answered on 27 Sep 2010, 10:42 AM
Hello Parth,

There is no alternative to this event. You have to upgrade to RadControls for ASP.NET AJAX version 2008 Q2 SP1 (2008.2.826) or later.

Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
wnl
Top achievements
Rank 1
answered on 08 Nov 2011, 04:30 PM
Hi,
I tried this solution but I don't have polish chars in the xls file.
But wtih "save as" dialog the file is correct.
Why?
0
Daniel
Telerik team
answered on 08 Nov 2011, 05:06 PM
Please modify the GridExporting event this way and let me know what happens:
protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
{
    string path = Server.MapPath("~/MyFile.xls");
    using (FileStream fs = File.Create(path))
    {
        Byte[] info = System.Text.Encoding.Unicode.GetBytes(e.ExportOutput);
        fs.Write(info, 0, info.Length);
    }
    Response.Redirect(Request.Url.ToString());
}

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
0
wnl
Top achievements
Rank 1
answered on 09 Nov 2011, 09:56 AM
Your code didn't help. Still without polish chars.
0
Daniel
Telerik team
answered on 11 Nov 2011, 01:45 PM
Hello Wnl,

Please try to reproduce the problem with Firefox and Chrome and let me know what happens.

Thanks,
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
0
RBarnes
Top achievements
Rank 1
answered on 16 Oct 2012, 02:31 PM
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.

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
0
Kostadin
Telerik team
answered on 22 Oct 2012, 12:17 PM
Hi Roger,

Please note that the required functionality for exporting three RadGrid controls at one request is not a supported scenario. In order to achieve the required functionality you should perform three separate requests, one for each specific control.

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
RBarnes
Top achievements
Rank 1
answered on 22 Oct 2012, 12:53 PM
I think I'm confused, that or I didn't explain the issue well enough.

How am I suppose to perform three seperate request, one for each control then, other than the way that I'm doing it.

Even if I try to export with a single grid, it doesn't get exported either, if there is additonal code that follows.

However, I can exec a command event in the command item of each grid and it exports fine.

I also posted this same topic on another thread,   I have some additional testing results in that thread.

I have a button click event.
    Export Grid 1 to excel

    Export Grid 2 to excel

    Export Grid 3 to excel

    Additional code to be processed after all three grids are exported.


0
Kostadin
Telerik team
answered on 23 Oct 2012, 03:45 PM
Hi Roger,

I answered your other forum post and you can see the reply here.

Kind 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.
0
Clint
Top achievements
Rank 1
answered on 15 Apr 2013, 06:21 PM
Hi,

I downloaded the sample code and it worked perfectly in my intranet application if I used FireFox browser.  However, it is not working when I used IE 8 browser.  The intranet application is using Windows authentication mode.  I am using version 2010.3.1109.20.

Thanks,
Clint
 
0
Kostadin
Telerik team
answered on 18 Apr 2013, 08:57 AM
Hi Clint,

I am not completely sure whether you are talking about the sample which Daniel provided in his reply. If that is the example I tried it with the same version of Telerik controls and it is working properly under IE 8. Could you please elaborate a little bit more on your scenario? Sending us your code declaration and the related code behind could help us to pinpoint the reason for that behavior.

Kind 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.
0
Clint
Top achievements
Rank 1
answered on 22 Apr 2013, 01:33 PM
Hi Kostadin,

Thanks very much for your response.  I figured out the reason I was not able to achieve what you most people can with the same codes.  The web config file has SmartNavigation flag set to true that causes this issue. 

Thanks,
Clint
0
Allen
Top achievements
Rank 2
Iron
Veteran
answered on 05 Jun 2015, 05:13 PM
Why do we have to do any of this?  Shouldn't the file just magically appear wherever we said the FileName property should be?
0
Kostadin
Telerik team
answered on 10 Jun 2015, 11:01 AM
Hi Allen,

I am afraid I am not understand your question. In case you need to save the grid to the server you can use the approach from the following example. Otherwise could you please elaborate a little bit more on your requirement?

Regards,
Kostadin
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Tags
Grid
Asked by
Ajay Bhonsule
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Parth
Top achievements
Rank 1
wnl
Top achievements
Rank 1
RBarnes
Top achievements
Rank 1
Kostadin
Telerik team
Clint
Top achievements
Rank 1
Allen
Top achievements
Rank 2
Iron
Veteran
Share this question
or