如何以pdf格式发送水晶报告

时间:2012-11-01 10:47:51

标签: c# crystal-reports

我是新来的,这是我的第一篇文章,如果有任何错误原谅我并且让我知道我会纠正它。 我的问题是我需要通过电子邮件将水晶报告发送给特定收件人的电子邮件地址,该地址也将包含在水晶报告中,并且该水晶报告需要采用PDF格式,这是可能的,如果是,那么请您详细说明如何处理做到这一点。

感谢。

3 个答案:

答案 0 :(得分:2)

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;

答案 1 :(得分:0)

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;
    }
}`

使用上面的方法你会解决问题

答案 2 :(得分:0)

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使用邮件概念你将发送它的邮件