自动生成报告(每周和每月)

时间:2011-12-01 10:06:57

标签: c# crystal-reports

有谁知道如何为每周和每月报告安排自动生成?我尝试使用Windows服务,但问题是我不知道如何设置我的报告以自动生成每周和每月报告。此时,我只将日期设置为setparametervalue。如果有人可以指导我这个并在下面提到我的编码,我将不胜感激。感谢。

    private Timer scheduleTimer = null;
    private DateTime lastRun;
    private bool flag;

    static TableLogOnInfo crTableLogonInfo;
    static ConnectionInfo crConnectionInfo;
    static Tables crTables;
    static Database crDatabase;

    static string Server = ConfigurationManager.AppSettings["serverName"];
    static string Database = ConfigurationManager.AppSettings["databaseName"];
    static string UserID = ConfigurationManager.AppSettings["userID"];
    static string Password = ConfigurationManager.AppSettings["password"];

    public ReportWindowsService()
    {
        InitializeComponent();

        scheduleTimer = new Timer();
        scheduleTimer.Interval = 1 * 5 * 60 * 1000;
        scheduleTimer.Elapsed += new ElapsedEventHandler(scheduleTimer_Elapsed);

    }

    protected override void OnStart(string[] args)
    {
        // TODO: Add code here to start your service.
        flag = true;
        lastRun = DateTime.Now;
        scheduleTimer.Start();
    }

    protected void scheduleTimer_Elapsed(object sender, ElapsedEventArgs e)
    {
        if (flag == true)
        {
            BillingReport();
            ReleaseImageReport();
            lastRun = DateTime.Now;
            flag = false;
        }
        else if (flag == false)
        {
            if (lastRun.Date < DateTime.Now.Date)
            {
                BillingReport();
                ReleaseImageReport();
            }
        }
    }

    public static ConnectionInfo ReportLogin(ReportDocument crRpt)
    {
        crConnectionInfo = new ConnectionInfo();
        crConnectionInfo.ServerName = Server;
        crConnectionInfo.DatabaseName = Database;
        crConnectionInfo.UserID = UserID;
        crConnectionInfo.Password = Password;

        crDatabase = crRpt.Database;
        crTables = crDatabase.Tables;
        foreach (CrystalDecisions.CrystalReports.Engine.Table crTable in crTables)
        {
            crTableLogonInfo = crTable.LogOnInfo;
            crTableLogonInfo.ConnectionInfo = crConnectionInfo;
            crTable.ApplyLogOnInfo(crTableLogonInfo);
        }

        return crConnectionInfo;
    }

    public static void BillingReport()//monthly report
    {
        ReportDocument crRpt = new ReportDocument();
        TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
        TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        crRpt.Load("C:\\rptBilling.rpt");

        ReportLogin(crRpt);

        crRpt.SetParameterValue("@CollectionStartDate", "2011/09/14");
        crRpt.SetParameterValue("@CollectionEndDate", "2011/09/29");
        crRpt.SetParameterValue("@SpokeCode", "14");
        crRpt.SetParameterValue("@UseCollectionDate", "1");//value can be set 0 or 1
        crRpt.SetParameterValue("@UseUploadDate", "0");//value can be set 0 or 1
        crRpt.SetParameterValue("@UploadStartDate", "2011/09/23");
        crRpt.SetParameterValue("@UploadEndDate", "2011/09/23");

        crRpt.ExportToDisk(ExportFormatType.PortableDocFormat, "e:\\BillingReport.pdf");
    }

    public static void ReleaseImageReport()//weekly report
    {
        ReportDocument crRpt = new ReportDocument();
        TableLogOnInfos crtableLogoninfos = new TableLogOnInfos();
        TableLogOnInfo crtableLogoninfo = new TableLogOnInfo();
        ConnectionInfo crConnectionInfo = new ConnectionInfo();
        crRpt.Load("C:\\rptReleaseImage.rpt");

        ReportLogin(crRpt);

        crRpt.SetParameterValue("@CollectionStartDate", "2011/09/14");
        crRpt.SetParameterValue("@CollectionEndDate", "2011/09/29");
        crRpt.SetParameterValue("@SpokeCode", "14");
        crRpt.SetParameterValue("@UseCollectionDate", "1");//value can be set 0 or 1
        crRpt.SetParameterValue("@UseUploadDate", "0");//value can be set 0 or 1
        crRpt.SetParameterValue("@UploadStartDate", "2011/09/23");
        crRpt.SetParameterValue("@UploadEndDate", "2011/09/23");

        crRpt.ExportToDisk(ExportFormatType.PortableDocFormat, "e:\\ReleaseImageReport.pdf");
    }

    protected override void OnStop()
    {
        // TODO: Add code here to perform any tear-down necessary to stop your service.
        scheduleTimer.Stop();
    }

2 个答案:

答案 0 :(得分:1)

为什么不将Windows任务调度程序与每周或每月接收的控制台应用程序一起用作参数?

答案 1 :(得分:0)

您可以使用SQL-JOB从定期日期时间和run the exe开始。

  1. 在这种情况下,您可以将 Query parameter 作为 Run-time Parameter 发送。
  2. 您还必须 write an App 才能获取参数 Run report based on input parameter and then Save it