I have worked on scheduling Crystal reports using ASP.NET, and it is been great learning.
It has lot of cool features exposed. We can set the type of report format we want and even we can customize that.
Steps
1)Open Session
2)Get Report Object
3)Configure new Report (Type, Occurence)
4)Set Parameter
SessionMgr ceSessionMgr = new SessionMgr();
EnterpriseSession ceSession;
ceSession = ceSessionMgr.Logon(AppSettings.GetDatabaseConnectionString("CrystalUserName"), AppSettings.GetDatabaseConnectionString("CrystalPassword"), AppSettings.GetDatabaseConnectionString("CrystalServerName"), "secEnterprise");
if (ceSession != null)
{
ceEnterpriseService = ceSession.GetService("", "InfoStore");
ceInfoStore = new InfoStore(ceEnterpriseService);
strQuery = "Select * From CI_INFOOBJECTS Where SI_PROGID = 'CrystalEnterprise.Report' AND SI_NAME='" + REPORTNAME + "'";
ceReportObjects = ceInfoStore.Query(strQuery);
if (ceReportObjects.Count > 0)
{
ScheduleCrystal(ceReportObjects);
}
}
private void ScheduleCrystal(InfoObjects infoObjects)
{
Report report;
Destination destination;
Smtp smtp;
SmtpOptions smtpOptions;
SchedulingInfo schedulingInfo;
string strFromEmail = "xxx@xx.com";
string strToEmail = string.Empty;
string strSubject = string.Empty;
string strBody = string.Empty;
bool blPDF = false;
string strServerType = string.Empty;
try
{
// if env is dev then DEVEmailAddress otherwise actual analyst mail address
strToEmail = AppSettings.GetDatabaseConnectionString("DEVEmailAddress");
report = (Report)infoObjects[1].GetPluginInterface("Report");
if (blPDF)
{
report.ReportFormatOptions.Format = CeReportFormat.ceFormatPDF;
}
else
{
report.ReportFormatOptions.Format = CeReportFormat.ceFormatExcel;
report.ReportFormatOptions.ExcelFormat.ExportPageHeaderFooter = CeReportHeaderFooterOption.ceReportHeaderFooterOnce;
report.ReportFormatOptions.ExcelFormat.BaseAreaType = CeSectionType.ceDetail;
report.ReportFormatOptions.ExcelFormat.CreatePageBreak = false;
report.ReportFormatOptions.ExcelFormat.ExportAllPages = true;
report.ReportFormatOptions.ExcelFormat.ExportShowGridlines = true;
//report.ReportFormatOptions.ExcelFormat.IsTabularFormat = true;
}
strSubject = "Holdings Report - " + txtReportName.Text.Trim();
strBody = "Holdings Report for the following Tickers and Sectors \n\n";
strBody += "Report Name: " + txtReportName.Text.Trim() + "\n\n";
strBody += "Tickers: " + GetTickers() + "\n\n";
strBody += "Sectors: " + GetSectorNames(GetSectorIds()) + "\n";
SetParameters(report.ReportParameters);
schedulingInfo = infoObjects[1].SchedulingInfo;
//change the below code according to the selection
SetSchedulingTypeandDateTime(ref schedulingInfo);
destination = schedulingInfo.Destination;
smtp = (Smtp)infoObjects.InfoStore.Query("SELECT * FROM CI_SYSTEMOBJECTS WHERE SI_PARENTID=29 AND SI_NAME='CrystalEnterprise.Smtp'")[1].GetPluginInterface("");
smtpOptions = (SmtpOptions)smtp.ScheduleOptions;
foreach (string strEmail in strToEmail.Split(';'))
{
smtpOptions.ToAddresses.Add(strEmail);
}
//smtpOptions.CCAddresses.Add(strFromEmail);
smtpOptions.Subject = strSubject;
smtpOptions.Message = strBody;
smtpOptions.SenderAddress = strFromEmail;
smtpOptions.EnableAttachments = true;
if (blPDF)
{
smtpOptions.Attachments.Add("application/pdf", "NBRHoldingsReport_" + txtReportName.Text.Trim().Replace(" ", "") + ".pdf");
}
else
{
smtpOptions.Attachments.Add("application/vnd.ms-excel", "NBRHoldingsReport_" + txtReportName.Text.Trim().Replace(" ", "") + ".xls");
}
destination.Clear();
destination.Name = "CrystalEnterprise.Smtp";
destination.SetFromPlugin(smtp);
infoObjects.InfoStore.Schedule(infoObjects);
}
catch (Exception ex)
{
//throw;
}
}
private void SetParameters(ReportParameters parameters)
{
string strToEmail = string.Empty;
try
{
ReportParameterSingleValue singleValue;
ReportParameter reportParameter;
//for tickers
reportParameter = parameters[1];
singleValue = reportParameter.CreateSingleValue();
singleValue.Value = GetTickers();
reportParameter.CurrentValues.Clear();
reportParameter.CurrentValues.Add(singleValue);
}
catch (Exception ex){//throw;}
}
No comments:
Post a Comment