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

Programmatically exporting report to Excel directly from Web App

2 Answers 550 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Jill
Top achievements
Rank 1
Jill asked on 25 Jul 2012, 01:26 AM
When opening a report in xlsx using Q1'12 controls, this is throwing an error:  Thread was being aborted.  It then prompts me to open or save it, then says "it couldn't be downloaded".  But when I click "retry" it opens fine in excel as xlsx.

Here is my code: 
var reportProcessor = new Telerik.Reporting.Processing.ReportProcessor();
                Telerik.Reporting.Processing.RenderingResult result = reportProcessor.RenderReport("xlsx", reportToExport, null);
 
                string fileName = reportName + ".xlsx";
                Response.Clear();
                Response.Charset = "";
                Response.ContentType = "application/vnd.xlsx";
                Response.Cache.SetCacheability(HttpCacheability.Private);
                Response.Expires = -1;
                Response.Buffer = false;
                Response.AddHeader("Content-Disposition",string.Format("{0};FileName=\"{1}\"", "attachment", fileName));
                Response.OutputStream.Write(result.DocumentBytes, 0, result.DocumentBytes.Length);
                Response.End();

2 Answers, 1 is accepted

Sort by
0
Steve
Telerik team
answered on 27 Jul 2012, 02:38 PM
Hello Jill,

We have not been able to reproduce any problems with your code and the latest Q2 internal build. So our recommendation is to upgrade to the latest version and change your code like this to see if you would still be having problems:

void ExportReport(Telerik.Reporting.Report reportToExport)
       {
           Telerik.Reporting.InstanceReportSource instanceReportSource = new Telerik.Reporting.InstanceReportSource();
           instanceReportSource.ReportDocument = reportToExport;
           ReportProcessor reportProcessor = new ReportProcessor();
           RenderingResult result = reportProcessor.RenderReport("XLSX", instanceReportSource, null);
 
           string fileName = result.DocumentName + "." + result.Extension;
 
           Response.Clear();
           Response.ContentType = result.MimeType;
           Response.Cache.SetCacheability(HttpCacheability.Private);
           Response.Expires = -1;
           Response.Buffer = true;
 
           Response.AddHeader("Content-Disposition",
                              string.Format("{0};FileName=\"{1}\"",
                                            "attachment",
                                            fileName));
 
           Response.BinaryWrite(result.DocumentBytes);
           Response.End();
       }


Regards,
Steve
the Telerik team

BLOGGERS WANTED! Write a review about Telerik Reporting or the new Report Designer, post it on your blog and get a complimentary license for Telerik Reporting. We’ll even promote your blog and help bring you a few fresh readers. Yes, it’s that simple. And it’s free. Get started today >

0
David
Top achievements
Rank 1
answered on 30 Nov 2012, 08:13 PM
I encountered the same strange behavior when writing XLSX files to an HTTP response (in a plain ASP.net website, not a Telerik product). When I called Response.End() the "Thread was being aborted" exception was thrown.

This behavior was strange in that if I wrote a XLS file in the same way, it was not a problem. This is not specific to Sitefinity or Telerik. I was able to fix my problem by removing the "Response.End()" call and let the response end on it's own. Others on the internet have encourntered this error by a Server.Transfer or a Response.Redirect. Others catch the ThreadAbortException and swallow the error, and just move on.

Anyhow, I am hoping others may benefit from this, as I found this thread while researching my own problem.

~DavB
Tags
General Discussions
Asked by
Jill
Top achievements
Rank 1
Answers by
Steve
Telerik team
David
Top achievements
Rank 1
Share this question
or