我自己做的一个水晶报表查询、打印和导出程序。查询没问题但打印和输出没反应。修改程序,在
if (!Page.IsPostBack)
        {
            
            DDL_y1.Enabled = false;
            DDL_y2.Enabled = false;
            DDL_m1.Enabled = false;
            DDL_m2.Enabled = false;
            DDL_d1.Enabled = false;
            DDL_d2.Enabled = false;
            DDL_h1.Enabled = false;
            DDL_h2.Enabled = false;
            DDL_mn1.Enabled = false;
            DDL_mn2.Enabled = false;
            DDL_fl.Enabled = false;
            BTN_src.Enabled = false;
        }
        else
        {
            //选择查询方式
            if (RB1.Checked)
            {
                Get();
                DDL_fl.Enabled = true;
                BTN_src.Enabled = true;
            }
            else
            {
                Get();
                DDL_y1.Enabled = true;
                DDL_y2.Enabled = true;
                DDL_m1.Enabled = true;
                DDL_m2.Enabled = true;
                DDL_d1.Enabled = true;
                DDL_d2.Enabled = true;
                DDL_h1.Enabled = true;
                DDL_h2.Enabled = true;
                DDL_mn1.Enabled = true;
                DDL_mn2.Enabled = true;
                DDL_fl.Enabled = false;
                BTN_src.Enabled = true;
            }
加入Get();后可以打印,但打印和导出的是数据库表中全部内容,无法按查询打印相关内容。求各位高手帮忙,下面是全部代码:using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using ReportClassLibrary1;
public partial class Report : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //是否返回给服务器
        if (!Page.IsPostBack)
        {
            
            DDL_y1.Enabled = false;
            DDL_y2.Enabled = false;
            DDL_m1.Enabled = false;
            DDL_m2.Enabled = false;
            DDL_d1.Enabled = false;
            DDL_d2.Enabled = false;
            DDL_h1.Enabled = false;
            DDL_h2.Enabled = false;
            DDL_mn1.Enabled = false;
            DDL_mn2.Enabled = false;
            DDL_fl.Enabled = false;
            BTN_src.Enabled = false;
        }
        else
        {
            //选择查询方式
            if (RB1.Checked)
            {
                Get();
                DDL_fl.Enabled = true;
                BTN_src.Enabled = true;
            }
            else
            {
                Get();
                DDL_y1.Enabled = true;
                DDL_y2.Enabled = true;
                DDL_m1.Enabled = true;
                DDL_m2.Enabled = true;
                DDL_d1.Enabled = true;
                DDL_d2.Enabled = true;
                DDL_h1.Enabled = true;
                DDL_h2.Enabled = true;
                DDL_mn1.Enabled = true;
                DDL_mn2.Enabled = true;
                DDL_fl.Enabled = false;
                BTN_src.Enabled = true;
            }
        }
    }    //查询全部内容的调用
    private void Get()
    {
        string strProvider = "Server=(local);DataBase=demo;UID=sa;PWD=sa";
        cpReport oCR = new cpReport();
        DataSet1 ds = new DataSet1();
        SqlConnection MyConn = new SqlConnection(strProvider);
        MyConn.Open();
        string strSel = "Select  * from Products";
        SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
        MyAdapter.Fill(ds, "Products");
        oCR.SetDataSource(ds);
        this.CrystalReportViewer1.ReportSource = oCR;
    }
    
    //按分类查询的调用
    private void Get(string strFl)
    {
        string strProvider = "Server=(local);DataBase=demo;UID=sa;PWD=sa";
        cpReport oCR = new cpReport();
        DataSet1 ds = new DataSet1();
        SqlConnection MyConn = new SqlConnection(strProvider);
        MyConn.Open();
        string strSel = "Select * from Products where 供应商 ='" + strFl + "'";
        SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
        MyAdapter.Fill(ds, "Products");
        oCR.SetDataSource(ds);
        this.CrystalReportViewer1.ReportSource = oCR;
    }
    protected void BTN_src_Click(object sender, EventArgs e)
    {        if (RB1.Checked)
        {
            if (DDL_fl.SelectedValue.Equals("全部"))
            {
                Get();
            }
            else
            {
                Get(DDL_fl.SelectedValue.ToString());
            }
        }
        //按时间查询的调用
        else
        {
            string strProvider = "Server=(local);DataBase=demo;UID=sa;PWD=sa";
            cpReport oCR = new cpReport();
            DataSet1 ds = new DataSet1();
            SqlConnection MyConn = new SqlConnection(strProvider);
            MyConn.Open();            string STy1 = DDL_y1.SelectedValue.ToString();
            string STy2 = DDL_y2.SelectedValue.ToString();
            string STm1 = DDL_m1.SelectedValue.ToString();
            string STm2 = DDL_m2.SelectedValue.ToString();
            string STd1 = DDL_d1.SelectedValue.ToString();
            string STd2 = DDL_d2.SelectedValue.ToString();
            string STh1 = DDL_h1.SelectedValue.ToString();
            string STh2 = DDL_h2.SelectedValue.ToString();
            string STmn1 = DDL_mn1.SelectedValue.ToString();
            string STmn2 = DDL_mn2.SelectedValue.ToString();
            string STs1 = "0";
            string STs2 = "0";            DateTime StartTime = new DateTime(int.Parse(STy1), int.Parse(STm1), int.Parse(STd1), int.Parse(STh1), int.Parse(STmn1), int.Parse(STs1));
            DateTime EndTime = new DateTime(int.Parse(STy2), int.Parse(STm2), int.Parse(STd2), int.Parse(STh2), int.Parse(STmn2), int.Parse(STs2));            string strSel = "Select * from Products where 进货时间>='" + StartTime.ToShortDateString() + "' and 进货时间<='" + EndTime.ToShortDateString() + "'";
            SqlDataAdapter MyAdapter = new SqlDataAdapter(strSel, MyConn);
            MyAdapter.Fill(ds, "Products");
            oCR.SetDataSource(ds);
            this.CrystalReportViewer1.ReportSource = oCR;
        }
    }
}

解决方案 »

  1.   

    下面的程序可以将水晶报表打印到PDF中,
    ReportDocument objReport = new ReportDocument(); SqlConnection objSqlConnection = new SqlConnection(_strConnection);
    SqlCommand objSqlCommand = objSqlConnection.CreateCommand();
    SqlDataAdapter objSqlDataAdapter = new SqlDataAdapter();
    objSqlConnection.Open();

    CostOfSalesReport objCostOfSalesReport = new CostOfSalesReport();
    CostOfSalesDataSet objCostOfSalesDataSet = new CostOfSalesDataSet(); objCostOfSalesDataSet.EnforceConstraints = false; objSqlCommand.CommandText = "SELECT SUM(SDI.Quantity) SummaryQuantity, CONVERT(Decimal(10, 2), SUM(P.UnitCost * SDI.Quantity)) SummaryCost, CONVERT(Decimal(10, 2), SUM(SOI.Price * SDI.Quantity)) SummaryPrice, CONVERT(Decimal(10, 2), SUM(SOI.Price * SDI.Quantity) - SUM(P.UnitCost * SDI.Quantity)) SummaryMargin, CONVERT(Decimal(10, 2), (CASE WHEN SUM(SOI.Price * SDI.Quantity) = 0 THEN 0 ELSE (SUM(SOI.Price * SDI.Quantity) - SUM(P.UnitCost * SDI.Quantity)) * 100 / SUM(SOI.Price * SDI.Quantity) END) ) AverageMargin FROM SalesDeliveryItem SDI, SalesDelivery SD, PRODUCT P, SalesOrder SO, SalesOrderItem SOI WHERE SDI.SalesDeliveryid = SD.id AND SDI.Productid = P.id AND SD.SalesOrderid = SO.id AND SO.id = SOI.SalesOrderid AND SOI.Productid = P.id AND SO.Typeid <> 3";
    if (!strDate.Equals(""))
    {
    objSqlCommand.CommandText = objSqlCommand.CommandText + strDate;
    }
    objSqlDataAdapter.SelectCommand = objSqlCommand;
    objSqlDataAdapter.Fill(objCostOfSalesDataSet, "PageHeader");
    objSqlCommand.CommandText = "SELECT COUNT(SDI.id) TotalRecord FROM SalesDeliveryItem SDI, SalesDelivery SD, PRODUCT P, SalesOrder SO, SalesOrderItem SOI WHERE SDI.SalesDeliveryid = SD.id AND SDI.Productid = P.id AND SD.SalesOrderid = SO.id AND SO.id = SOI.SalesOrderid AND SOI.Productid = P.id AND SO.Typeid <> 3";
    if (!strDate.Equals(""))
    {
    objSqlCommand.CommandText = objSqlCommand.CommandText + strDate;
    }
    objSqlDataAdapter.SelectCommand = objSqlCommand;
    objSqlDataAdapter.Fill(objCostOfSalesDataSet, "TotalRecords");
    objSqlCommand.CommandText = "SELECT (PG.ProductGroup + ':    ' + PG.Description) ProductGroupInformation, P.ProductRef ProductRef, SDI.Quantity Qty, CONVERT(Decimal(10, 2), (SDI.Quantity * P.UnitCost)) SalesCost, CONVERT(Decimal(10, 2), (SDI.Quantity * SOI.Price)) SalesNet, CONVERT(Decimal(10, 2), (SDI.Quantity * SOI.Price - SDI.Quantity * P.UnitCost)) MarginAmount, CONVERT(Decimal(10, 2), CASE (SDI.Quantity * SOI.Price) WHEN 0 THEN 0 ELSE (100 * (SDI.Quantity * SOI.Price - SDI.Quantity * P.UnitCost)/(SDI.Quantity * SOI.Price)) END) MarginAge, SO.WraxallName OrderID, SD.DeliveryNO DenoteID, SD.InvoiceNo InvoiceNO, CONVERT(varchar(12), SD.InvoiceDate, 103) InvoiceDate, SO.CustomerName CustomerID FROM SalesDeliveryItem SDI, SalesDelivery SD, PRODUCT P, ProductGroup PG, SalesOrder SO, SalesOrderItem SOI WHERE SDI.SalesDeliveryid = SD.id AND SDI.Productid = P.id AND SD.SalesOrderid = SO.id AND SO.id = SOI.SalesOrderid AND SOI.Productid = P.id AND PG.ProductGroup = P.ProductGroup AND SO.Typeid <> 3";
    if (!strDate.Equals(""))
    {
    objSqlCommand.CommandText = objSqlCommand.CommandText + strDate;
    }
    objSqlDataAdapter.SelectCommand = objSqlCommand;
    objSqlDataAdapter.Fill(objCostOfSalesDataSet, "MainPart");
    objSqlConnection.Close();
    objCostOfSalesReport.SetDataSource(objCostOfSalesDataSet); //报表的参数赋值,来源是页面用户输入
    CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions ParamFields; 
    CrystalDecisions.Shared.ParameterValues curValues=new CrystalDecisions.Shared.ParameterValues();
    ParameterDiscreteValue paramDate = new ParameterDiscreteValue ();
    ParameterFieldDefinition paramField;

    ParamFields = objCostOfSalesReport.DataDefinition.ParameterFields; paramField = ParamFields["FromDate"];
    paramDate.Value = this.txtFromDate.Text.Trim();
    curValues.Add(paramDate);
    paramField.ApplyCurrentValues(curValues); paramField = ParamFields["ToDate"];
    paramDate.Value = this.txtToDate.Text.Trim();
    curValues.Add(paramDate);
    paramField.ApplyCurrentValues(curValues); objReport = objCostOfSalesReport;   CrystalDecisions.Shared.DiskFileDestinationOptions objDiskOpt = new CrystalDecisions.Shared.DiskFileDestinationOptions();
    objReport.ExportOptions.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile; string strOutputFileExtension = ".pdf";
    objReport.ExportOptions.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;

    string strFileName = "Cost Of Sales Report" + strOutputFileExtension;
    string strServerFilePath = Server.MapPath("../Files/Reports/") + strFileName;

    objDiskOpt.DiskFileName = strServerFilePath;
    objReport.ExportOptions.DestinationOptions = objDiskOpt;
    objReport.Export(); FileInfo objFileInfo = new FileInfo(strServerFilePath);
    Response.Clear();
    Response.AddHeader("Content-Disposition", "attachment; filename=" + objFileInfo.Name);
    Response.AddHeader("Content-Length", objFileInfo.Length.ToString());
    Response.ContentType = "application/octet-stream";
    Response.WriteFile(objFileInfo.FullName);
    Response.End();
      

  2.   

    太长不细看了
    但发现既然你加Get()可以打印,你何不修改Get()来符合要求呢?
    比如string strSel = "Select  * from Products";你可以修改为string strSel = "Select  * from Products where 你的查询条件";
      

  3.   

    代码太杂乱了
    实际上你 get(string str)就有你的查询条件的代码了,把你那些需要的查询条件放到这里面来  ////////////////////////
     if (RB1.Checked)
                {
                    Get(string sql);
                    DDL_fl.Enabled = true;
                    BTN_src.Enabled = true;
                }
                else
                {
                    Get();
                    DDL_y1.Enabled = true;
                    DDL_y2.Enabled = true;
                    DDL_m1.Enabled = true;
                    DDL_m2.Enabled = true;
                    DDL_d1.Enabled = true;
                    DDL_d2.Enabled = true;
                    DDL_h1.Enabled = true;
                    DDL_h2.Enabled = true;
                    DDL_mn1.Enabled = true;
                    DDL_mn2.Enabled = true;
                    DDL_fl.Enabled = false;
                    BTN_src.Enabled = true;
                }
            }