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

Export to Excel in code

5 Answers 593 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Doug Odegaard
Top achievements
Rank 2
Doug Odegaard asked on 11 Aug 2008, 10:18 PM
I have been searching for the right code to allow Telerik Reporting Q2 2008 to export the contents of a report to Excel without displaying it in an ASP.NET viewer.  I simply need to hit a button in the webapp and email an attached Excel spreadsheet created in Reporting.  Does anyone have some code or an example.  I have successfully done TIFFs and PDFs this way but not XLS.

Thanks in advance.
Doug

5 Answers, 1 is accepted

Sort by
0
Accepted
Rossen Hristov
Telerik team
answered on 12 Aug 2008, 08:00 AM
Hi Doug Odegaard,

You can call the following method on your button click:

void MailReport(Telerik.Reporting.Report report, string from, string to, string subject, string body)  
   {  
      string mimeType;  
      string extension;  
      Encoding encoding;  
  
      byte[] reportBytes =  
      ReportProcessor.Render("XLS", report, nullout mimeType, out extension, out encoding);  
            
      MemoryStream ms = new MemoryStream(reportBytes);  
      ms.Position = 0;  
             
      Attachment attachment = new Attachment(ms, report.Name + "." + extension);  
      MailMessage msg = new MailMessage(from, to, subject, body);  
      msg.Attachments.Add(attachment);  
      SmtpClient client = new SmtpClient(smptHost);  
      client.Send(msg);                         
   }  

I hope that this will help.

Greetings,
Ross
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Doug Odegaard
Top achievements
Rank 2
answered on 12 Aug 2008, 01:07 PM
Ross,
That worked perfectly....thank you.  One last question and I can open a separate post if needed.  I have added a Property to my report called ScheduleDate and followed the instructions from Lino's video for Design-Time Parameters.  It only work if I run the viewer and manually type in the parameter and hit 'refresh' to populate a textbox in the report that uses the parameter.  Strangely enough the SQLCommand gets the parameter from the code but not the text box.  Any ideas?  Does something need to be called for the Render method to recognize the property in the report def?  Here is the report def Property

 
    using System.ComponentModel;  
    using System.Drawing;  
    using System.Windows.Forms;  
    using Telerik.Reporting;  
    using Telerik.Reporting.Drawing;  
    using System;  
 
    /// <summary>  
    /// Summary description for ExcelExport.  
    /// </summary>  
    public partial class ExcelExport : Telerik.Reporting.Report  
    {  
        public ExcelExport()  
        {  
            /// <summary>  
            /// Required for telerik Reporting designer support  
            /// </summary>  
            InitializeComponent();  
 
            //  
            // TODO: Add any constructor code after InitializeComponent call  
            //  
        }  
 
        public DateTime ScheduleDate  
        {  
            get 
            {  
                return (DateTime)sqlDataAdapter1.SelectCommand.Parameters[0].Value;  
            }  
            set 
            {  
                sqlDataAdapter1.SelectCommand.Parameters[0].Value = value;  
            }  
        }  
    } 


Here is the calling routine using your MailReport method above....

       Email email = new Email();  
        ExcelExport reportMS = new ExcelExport();  
        reportMS.ScheduleDate = Convert.ToDateTime(Session["ScheduleDate"]);  
        email.MailReport(reportMS, "support@intralogix.biz""doug@intralogix.biz""Daily Cases for " + Session["ScheduleDate"] + " telerik code ""Attached is today's schedule");  
 
 



Thanks in advance.
Doug
0
Rossen Hristov
Telerik team
answered on 12 Aug 2008, 01:38 PM
Hi Doug Odegaard,

I may be wrong, but I think that you are confusing report parameters (Report.ReportParameters) with custom report properties. ReportParameters are a member of the Report class and are used for the automatic parameter UI that you see in a Viewer and which let's you specify report parameters. However report properties that you add by hand are not recognized "automatically" like parameters since they are part of your class and not of Telerik Reporting.

I am a little bit confused by the way. Are you using the Viewer or are you exporting the report to Excel programatically? Because you sent us code that exports a report programatically and then you mention a Viewer and a TextBox I am a little perplexed. If you are exporting it programatically then you need to Fill your DataSet after you set the ScheduleDate property and before you export the report so that you will have the correct data.

If you are still experiencing problems it will be best if you send us the whole thing since we cannot infer what is going on by reading only fragments from the source code. Also, we will need a step-by-step instructions about what we should do in order to reproduce that behavior.

Sincerely yours,
Ross
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
0
Doug Odegaard
Top achievements
Rank 2
answered on 12 Aug 2008, 01:53 PM
The parameter from Lino's video is passing in a Parameter via Property to the SQLDataAdapter so I can now see with your explanation why it would not be the same as the ReportParameters.  I simply added the value passed into the adapter to the output and it is echoing back as a field.

The Viewer was only used for testing the report.  Behind the scenes export is what I am doing in production.  Sorry to confuse you....your explanation helped to put the pieces together for me.

Thanks.
Doug
0
Rossen Hristov
Telerik team
answered on 12 Aug 2008, 02:03 PM
Hello Doug,

I am happy to hear that. Drop me a line if something else pops up along the way.

Kind regards,
Ross
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
General Discussions
Asked by
Doug Odegaard
Top achievements
Rank 2
Answers by
Rossen Hristov
Telerik team
Doug Odegaard
Top achievements
Rank 2
Share this question
or