私はここで新しいです、そしてこれは私の最初の投稿です、何か間違いがあれば私を許してください、そして私がそれを訂正することを私に知らせてください。私の質問は、CrystalReportを特定の受信者の電子メールアドレスに電子メールで送信する必要があることです。これはCrystalReportにも含まれ、Crystal ReportはPDF形式である必要があります。可能であれば、手順を詳しく説明してください。やれ。
ありがとう。
私はここで新しいです、そしてこれは私の最初の投稿です、何か間違いがあれば私を許してください、そして私がそれを訂正することを私に知らせてください。私の質問は、CrystalReportを特定の受信者の電子メールアドレスに電子メールで送信する必要があることです。これはCrystalReportにも含まれ、Crystal Reportは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);
}
}
上記により、メールコンセプトを使用してCrystalReportを使用して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;
}
}`
上記の方法を使用すると、問題が解決されます