Wednesday, April 28, 2010

Programmatically Generate Report and store as pdf or send as email

Every time going to Reporting service and generate report it's tedious work also some report takes longer with millions of data. User does not have time to click report generate button and wait for 10-15 minutes. Use need ready to use report with real time data. We can schedule service or Task and generate report in background and user get email on his/her schedule time. Here I tried to create some application and it's running very well on my production environment.
below if only basic structure of code.
1) Create New Windows / Web Application (depend on your requirment)
2) Add Web Refrence for Web Site / Service Refrence for Windows
3) If you are using .Net 3.5 can use WCF client, instead of web service client.
4) For excel, csv,web and xml --> try file extetion as .".xls",".csv",".html",".xml"

using System.Text;
using System.Windows.Forms;
using System.IO;

using System.Net.Mail;

private rs2005.ReportingService2005 rs;
private rsExecService.ReportExecutionService rsExec;

rs = new rs2005.ReportingService2005();
rsExec = new rsExecService.ReportExecutionService();

// Authenticate to the Web service using Windows credentials

rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;

//// Assign the URL of the Web service

rs.Url = "http://SERVERNAME/ReportServer/ReportService2005.asmx";
rsExec.Url = http://SERVERNAME/ReportServer/ReportExecution2005.asmx;

string historyID = null;
string deviceInfo = null;
string format = "pdf";

// Byte[] Sendresults;
string encoding = String.Empty;
string mimeType = String.Empty;
string extension = String.Empty;
rsExecService.Warning[] warnings = null;
string[] streamIDs = null;
// Default Path;
string fileName = @"c:\samplereport.pdf";
// Get the report name
string _reportName = @"/FOLDER/REPORT_NAME";
string _historyID = null;
bool _forRendering = false;

ParameterValue[] _values = null;
DataSourceCredentials[] _credentials = null;
ReportParameter[] _parameters = null;
// Load the selected report.
rsExecService.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);

// Prepare report parameter.
rsExecService.ParameterValue[] parameters =
new rsExecService.ParameterValue[2];
// Place to include the parameter.
if (_parameters.Length > 0)
parameters[0] = new rsExecService.ParameterValue();
parameters[0].Label = "StartDate";
parameters[0].Name = "StartDate";
parameters[0].Value = DateTime.Now.Date.ToShortDateString();

parameters[1] = new rsExecService.ParameterValue();
parameters[1].Label = "EndDate";
parameters[1].Name = "EndDate";
parameters[1].Value = DateTime.Now.AddDays(1).ToShortDateString();
rsExec.SetExecutionParameters(parameters, "en-us");
//get pdf
byte[] Sendresults = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);

MeoryStream ms = new MemoryStream(Sendresults);
//send email
MailMessage mail = new MailMessage();
mail.Subject = "Test_Report " + DateTime.Now.ToString();
mail.From = new MailAddress("");
mail.Attachments.Add(new Attachment(ms, string.Format("{0}_Document." + format, "Test_Reprots")));
SmtpClient smtp = new SmtpClient("");

// Create a file stream and write the report as file
using (System.IO.FileStream stream = File.OpenWrite(fileName))
stream.Write(Sendresults, 0, Sendresults.Length);
catch (Exception ex)

No comments: