65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 C#.NET 2.0 在 Windows 应用程序中使用 Crystal Report 创建标签

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.86/5 (6投票s)

2008年7月20日

CPOL

2分钟阅读

viewsIcon

22765

一篇关于使用 C#.NET 2.0 在 Windows 应用程序中使用 Crystal Report 创建标签的文章

Labels.jpg

引言

大家好。我用 Crystal Report 编写了此标签创建代码。最初,有人建议我使用文本报告来完成此任务。但文本报告的缺点是,我每次只能生成一个标签。现在,我创建了一个报告,可以为单个样本生成多个标签,并以此类推。我使用 Visual Studio .NET Windows 应用程序进行编码,后端使用的是 Oracle 10g。

Using the Code

在开始编码之前,您需要为我们创建标签的规范创建表(Oracle 或 My SQL)。在 Visual Studio 中,在数据集中创建 14 个数据表。创建一个表单来选择标签详细信息,使用您喜欢的任何代码,如上图所示。在此屏幕上,您可以一次性获取标签,也可以逐个获取。通过选中该复选框,您可以一次性获取标签,或者逐个获取。您将看到的下面的代码仅属于“插入”按钮。 “checkclr”用于清除此屏幕上所做的检查,而“clear”按钮用于清除数据集。我使用连接类创建此代码。您自己打开与 Oracle 或 SQL 的连接,并尝试一下。

打开 Crystal Report,并将这 14 个数据表按照您希望打印的顺序放置在报告中。您在 if 循环中找到的名称 Label1 是物理数据集中数据表的名称。

代码中的“n”表示特定样本所需的标签数量。

if (checkBox5.Checked)
{
    string lblstr = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                    LABREPORT B WHERE A.DRUGCODE = B.DRUGCODE AND B.SAMPLECODE BETWEEN '"
                    + cmbsamplecd.Text + "' AND '" + cmbsampleto.Text
                    + "' ORDER BY SAMPLECODE";
    DataTable lbldt = Conn.ExecuteRead(lblstr);

    rptDataSet ds = new rptDataSet();
    if (radioButton20.Checked)
    {
        int n, m;//n = count of the label, m = count of samplecode (Harry Christopher)
        m = lbldt.Rows.Count;
        for (int k = 0; k <= m - 1; k++)
        {
            n = Convert.ToInt32(lbldt.Rows[k]["count"].ToString());
            if (j != 0)
            {
                n = n + j;
                try
                {
                    if (n < 18)
                    {
                        for (i = j; i <= n - 1; i++)
                        {
                            if (i == 1 && radioButton1.Checked != true
                                && ds.Label1.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE = 
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label1", query);
                                radioButton1.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;//This is used to stop the loop once all
                                              //the labels have been created.
                                              //This will lead to an exception, 
                                              //do not care,
                                              //still it works fine. (Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 2 && radioButton2.Checked != true && 
                                ds.Label2.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '" +
                                    lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label2", query);
                                radioButton2.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show
                                        ("Labels", new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop once
                                                //all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 3 && radioButton3.Checked != true && 
                                ds.Label3.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE = B.DRUGCODE
                                    AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label3", query);
                                radioButton3.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop once
                                                //all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 4 && radioButton4.Checked != true && 
                                ds.Label4.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE = B.DRUGCODE
                                    AND B.SAMPLECODE = '" +
                                    lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label4", query);
                                radioButton4.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop once
                                                //all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 5 && radioButton5.Checked != true && 
                                ds.Label5.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE = B.DRUGCODE
                                    AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label5", query);
                                radioButton5.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 6 && radioButton6.Checked != true && 
                                ds.Label6.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE = B.DRUGCODE
                                    AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label6", query);
                                radioButton6.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 7 && radioButton7.Checked != true && 
                                ds.Label7.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label7", query);
                                radioButton7.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 8 && radioButton8.Checked != true && 
                                ds.Label8.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                    rptOpen.SetData("label8", query);
                                radioButton8.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 9 && radioButton9.Checked != true && 
                                ds.Label9.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label9", query);
                                radioButton9.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 10 && radioButton10.Checked != true && 
                                ds.Label10.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label10", query);
                                radioButton10.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 11 && radioButton11.Checked != true && 
                                ds.Label11.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label11", query);
                                radioButton11.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 12 && radioButton12.Checked != true && 
                                ds.Label12.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label12", query);
                                radioButton12.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;  //This is used to stop the loop
                                                //once all the labels have been created.
                                                //This will lead to an exception, 
                                                //do not care,
                                                //still it works fine.(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 13 && radioButton13.Checked != true && 
                                ds.Label13.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label13", query);
                                radioButton13.Checked = true;
                                j = i + 1;
                                if (k == m - 1 && i == n - 1)
                                {
                                    rptOpen.Show("Labels", 
                                        new Forms.QC.Reports.QCLabels16());
                                    k = m + 1;    //This is used to stop the loop
                                                  //once all the labels have been created
                                                  //This will lead to an exception, 
                                                  //do not care,
                                                  //still it works fine. 
                                                  //(Harry Christopher)
                                    i = n + 1;
                                }
                            }
                            if (i == 14 && radioButton14.Checked != true && 
                                ds.Label14.Rows.Count == 0)
                            {
                                string query = "SELECT A.*, 
                                    B.SAMPLECODE FROM QCDRUGSPEC A,
                                    LABREPORT B WHERE A.DRUGCODE =
                                    B.DRUGCODE AND B.SAMPLECODE = '"
                                    + lbldt.Rows[k]["samplecode"].ToString() + "'";
                                rptOpen.SetData("label14", query);
                                radioButton14.Checked = true;
                                int v = n - i;    //This will help in not 
                                                  //breaking the loop
                                                  //while new page opens.
                                                  //This will make the loop to continue. 
                                                  //(Harry Christopher)
                                n = v;
                                i = 0;
                                j = i + 1;
                                rptOpen.Show("Labels", 
                                    new Forms.QC.Reports.QCLabels16());
                                //This query is to just fill the dataset 
                                //with null values,
                                //nothing special with this query.(Harry Christopher)
                                string query1 = 
                                "select address3 from labmaster where labcode = 'ABCD'";
                                rptOpen.SetData("label1", query1);
                                rptOpen.SetData("label2", query1);
                                rptOpen.SetData("label3", query1);
                                rptOpen.SetData("label4", query1);
                                rptOpen.SetData("label5", query1);
                                rptOpen.SetData("label6", query1);
                                rptOpen.SetData("label7", query1);
                                rptOpen.SetData("label8", query1);
                                rptOpen.SetData("label9", query1);
                                rptOpen.SetData("label10", query1);
                                rptOpen.SetData("label11", query1);
                                rptOpen.SetData("label12", query1);
                                rptOpen.SetData("label13", query1);
                                rptOpen.SetData("label14", query1);
                                rptOpen.SetData("label15", query1);
                                rptOpen.SetData("label16", query1);
                            }
                        }
                    }
                }
                catch
                {
                    MessageBox.Show("Labels are Ready!!! Go ahead!!!");
                }
            }
        }
    }
}
if (!checkBox5.Checked)
{
    rptDataSet ds = new rptDataSet();
    if (radioButton20.Checked)
    {
        int n;
        n = Convert.ToInt32(txtno.Text);

        if (j != 0)
        {
            n = n + j;
            try
            {
                if (n < 18)
                {
                    for (i = j; i <= n - 1; i++)
                    {
                        if (i == 1 && radioButton1.Checked != true && 
                            ds.Label1.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label1", query);
                            radioButton1.Checked = true;
                            j = i + 1;
                        }
                        if (i == 2 && radioButton2.Checked != true && 
                            ds.Label2.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label2", query);
                            radioButton2.Checked = true;
                            j = i + 1;
                        }
                        if (i == 3 && radioButton3.Checked != true && 
                            ds.Label3.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                                rptOpen.SetData("label3", query);
                            radioButton3.Checked = true;
                            j = i + 1;
                        }
                        if (i == 4 && radioButton4.Checked != true && 
                            ds.Label4.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label4", query);
                            radioButton4.Checked = true;
                            j = i + 1;
                        }
                        if (i == 5 && radioButton5.Checked != true && 
                            ds.Label5.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label5", query);
                            radioButton5.Checked = true;
                            j = i + 1;
                        }
                        if (i == 6 && radioButton6.Checked != true && 
                            ds.Label6.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                                rptOpen.SetData("label6", query);
                            radioButton6.Checked = true;
                            j = i + 1;
                        }
                        if (i == 7 && radioButton7.Checked != true && 
                            ds.Label7.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                                rptOpen.SetData("label7", query);
                            radioButton7.Checked = true;
                            j = i + 1;
                        }
                        if (i == 8 && radioButton8.Checked != true && 
                            ds.Label8.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label8", query);
                            radioButton8.Checked = true;
                            j = i + 1;
                        }
                        if (i == 9 && radioButton9.Checked != true && 
                            ds.Label9.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label9", query);
                            radioButton9.Checked = true;
                            j = i + 1;
                        }
                        if (i == 10 && radioButton10.Checked != true && 
                            ds.Label10.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label10", query);
                            radioButton10.Checked = true;
                            j = i + 1;
                        }
                        if (i == 11 && radioButton11.Checked != true && 
                            ds.Label11.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label11", query);
                            radioButton11.Checked = true;
                            j = i + 1;
                        }
                        if (i == 12 && radioButton12.Checked != true && 
                            ds.Label12.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label12", query);
                            radioButton12.Checked = true;
                            j = i + 1;
                        }
                        if (i == 13 && radioButton13.Checked != true && 
                            ds.Label13.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label13", query);
                            radioButton13.Checked = true;
                            j = i + 1;
                        }
                        if (i == 14 && radioButton14.Checked != true && 
                            ds.Label14.Rows.Count == 0)
                        {
                            string query = "SELECT A.*, B.SAMPLECODE FROM QCDRUGSPEC A,
                                LABREPORT B WHERE A.DRUGCODE =
                                B.DRUGCODE AND B.SAMPLECODE = '"
                                + cmbsamplecd.Text + "'";
                            rptOpen.SetData("label14", query);
                            radioButton14.Checked = true;
                            j = i + 1;
                        }
                    }
                }
            }
            catch
            {
                MessageBox.Show("You are running out of the count, 
                    Let go with next report.");
            }
        }
    }
}

关注点

我没有学到任何东西,但在创建这段代码时,我疯了整整一个月,因为我的前辈对此一无所知,并嘲笑我。我写这篇文章只是为了表明我不能被批评。希望您能理解上述描述。请提供您的反馈,以便我可以调整此代码以获得最佳性能。

历史

  • 2008年7月17日:初始版本
© . All rights reserved.