我是新来的,这是我的第一篇文章,如果有任何错误请原谅我,请告诉我,我会纠正它。我的问题是我需要通过电子邮件将水晶报告发送到特定收件人的电子邮件地址,该电子邮件地址也将包含在水晶报告中,并且水晶报告需要为 PDF 格式,这可能吗,如果可以,请详细说明如何做。
谢谢。
我是新来的,这是我的第一篇文章,如果有任何错误请原谅我,请告诉我,我会纠正它。我的问题是我需要通过电子邮件将水晶报告发送到特定收件人的电子邮件地址,该电子邮件地址也将包含在水晶报告中,并且水晶报告需要为 PDF 格式,这可能吗,如果可以,请详细说明如何做。
谢谢。
string pdfFile = "c:\\CrytalReport.pdf";
protected void Button1_Click(object sender, EventArgs e)
{
try
{
ExportOptions CrExportOptions;
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = pdfFile;
CrExportOptions = ReportDocument .ExportOptions;//Report document object has to be given here
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
ReportDocument .Export();
sendmail();
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
private void sendmail()
{
try
{
MailMessage Msg = new MailMessage();
Msg.To = "to Address";
Msg.From = "From Address";
Msg.Subject = "Crystal Report Attachment ";
Msg.Body = "Crystal Report Attachment ";
Msg.Attachments.Add(new MailAttachment(pdfFile));
// System.Web.Mail.SmtpMail.Send(Msg);
SmtpMail.SmtpServer = "you mail domain";
//SmtpMail.SmtpServer.Insert(0,"127.0.0.1");
SmtpMail.Send(Msg);
}
catch (Exception ex)
{
//MessageBox.Show(ex.ToString());
}
}
不要忘记添加这些 DLL
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
private void SetReportDataSource(string sCarrierID)
{
try
{
CrystalReport objCrystalReport = new CrystalReport();
//ReportDocument objReportDoc = new ReportDocument();
ConnectionInfo objConnInfo = new ConnectionInfo();
//string strConn = System.Configuration.ConfigurationSettings.AppSettings[Campaign];
string[] strArray = objCrystalReport.GetCampaignConn();
CrystalDecisions.Shared.TableLogOnInfo logOnInfo = new CrystalDecisions.Shared.TableLogOnInfo();
objConnInfo.DatabaseName = strArray[1].ToString();
objConnInfo.UserID = strArray[2].ToString();
objConnInfo.Password = strArray[3].ToString();
objConnInfo.ServerName = strArray[0].ToString();
String ReportPath = (Server.MapPath("Report") + @"\rptInvoice.rpt");
//String ReportPath = Server.MapPath("~//Report//VendorRegistration.rpt");
//objReportDoc = objCrystalReport.CrystalLogon(ReportPath, objConnInfo, ref logOnInfo);
//// for displaying Crystal report
//crVendorRegistration.ReportSource = objReportDoc;
//crVendorRegistration.ParameterFieldInfo[0].CurrentValues.Clear();
ParameterDiscreteValue pdvalue = new ParameterDiscreteValue();
pdvalue.Value = Convert.ToInt32(sCarrierID);
//crVendorRegistration.ParameterFieldInfo[0].CurrentValues.Add(pdvalue);
//crVendorRegistration.DataBind();
//// for Exporitng in PDF
ReportDocument repDoc = objCrystalReport.CrystalLogon(ReportPath, objConnInfo, ref logOnInfo);
repDoc.ParameterFields[0].CurrentValues.Add(pdvalue);
// Stop buffering the response
HttpContext.Current.Response.Buffer = false;
// Clear the response content and headers
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
// Export the Report to Response stream in PDF format and file name Customers
//repDoc.ExportToHttpResponse(ExportFormatType.PortableDocFormat, HttpContext.Current.Response, true, "Truck Invoice " + sCarrierID + "");
//repDoc.GenerateReport
String ReportGenerated = (Server.MapPath("GenerateReport") + @"\rptInvoice" + sCarrierID + ".pdf");
// There are other format options available such as Word, Excel, CVS, and HTML in the ExportFormatType Enum given by crystal reports
ExportOptions CrExportOptions = new ExportOptions();
DiskFileDestinationOptions CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
PdfRtfWordFormatOptions CrFormatTypeOptions = new PdfRtfWordFormatOptions();
CrDiskFileDestinationOptions.DiskFileName = ReportGenerated;
CrExportOptions = repDoc.ExportOptions;
CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
CrExportOptions.FormatOptions = CrFormatTypeOptions;
repDoc.Export(CrExportOptions);
//pdvalue = new ParameterDiscreteValue();
//pdvalue.Value = QC_By;
//crSalesRpt.ParameterFieldInfo[1].CurrentValues.Add(pdvalue);
//objReportDoc.VerifyDatabase();
//objReportDoc.Refresh();
}
catch (Exception ex)
{
//bool rethrow = ExceptionPolicy.HandleException(ex, "");
//if (rethrow)
// throw;
//Redirecting to error message page
//Server.Transfer(ConstantClass.StrErrorPageURL);
}
}
通过上面你使用水晶报告创建pdf通过使用邮件概念你将发送邮件就是这样
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
/// <summary>
/// Summary description for CrystalReport
/// </summary>
'public class CrystalReport
{
private void SetDBLogOnInfo(ConnectionInfo connectionInfo, ReportDocument objectReportDocument, TableLogOnInfo tableLogonInfo)
{
Tables Tbl = objectReportDocument.Database.Tables;
foreach (CrystalDecisions.CrystalReports.Engine.Table TmpTbl in Tbl)
{
tableLogonInfo = TmpTbl.LogOnInfo;
tableLogonInfo.ConnectionInfo = connectionInfo;
TmpTbl.ApplyLogOnInfo(tableLogonInfo);
}
}
public ReportDocument CrystalLogon(String pReportPath, ConnectionInfo objConnInfo, ref TableLogOnInfo tableLogonInfo)
{
ReportDocument objReportDoc = new ReportDocument();
objReportDoc.Load(pReportPath);
SetDBLogOnInfo(objConnInfo, objReportDoc, tableLogonInfo);
return objReportDoc;
}
public string[] GetCampaignConn()
{
//string server, database, userid, password;
string strConn = System.Configuration.ConfigurationSettings.AppSettings["Connection String"];
string[] strArray = strConn.Split(';');
string[] strOutput = new string[4];
for (int i = 0; i < strArray.Length; i++)
{
string[] strObject = strArray[i].Split('=');
if (strObject[0] == "Data Source")
{
strOutput[0] = strObject[1];
}
else if (strObject[0] == "Initial Catalog")
{
strOutput[1] = strObject[1];
}
else if (strObject[0] == "User ID")
{
strOutput[2] = strObject[1];
}
else if (strObject[0] == "Password")
{
strOutput[3] = strObject[1];
}
}
return strOutput;
}
}`
使用上面的方法你会解决问题