如何在VS.NET2003的报表中显示图片[子报表]

来源:互联网 发布:无线网络流量测试软件 编辑:程序博客网 时间:2024/04/29 23:50

上个月做项目遇到的问题,查了一些资料才搞定.可惜兄弟记性不好,忘记从哪学来的了.MMD都是美女给闹的

public static CRExamNoPrint crenp=new CRExamNoPrint();//报表文件

DataSet ds=new DataSet();

//一个按钮事件,这里会动态创建一个table,然后将内容显示在报表中

private void btnPrint_Click(object sender, System.EventArgs e)
  {
   try
   {
    string strCount="select count(xjh) from xs_ksxx where xjh='"+this.gXJH.Text.Trim()+"'";
    LocalDb ldb=new LocalDb();//LocalDb是数据访问类
    if(ldb.ExecScalar(strCount)==0)//不存在该学生
    {
     ((CrystalDecisions.CrystalReports.Engine.TextObject)crenp.ReportDefinition.ReportObjects["TxtTitle"]).Text="打印考生"+this.gXM.Text.Trim()+"的准考证";
     //建表
     this.ds.Tables.Clear();
     this.ds.Tables.Add("zkz");
     
     // 添加两个字段
     ds.Tables[0].Columns.Add("xjh", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("zkzh", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("xxdm", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("xm", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("xb", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("zp", System.Type.GetType("System.Byte[]"));
     ds.Tables[0].Columns.Add("kch", System.Type.GetType("System.Int32"));
     ds.Tables[0].Columns.Add("kc", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("zwh", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("kssj", System.Type.GetType("System.String"));
     ds.Tables[0].Columns.Add("jssj", System.Type.GetType("System.String"));
     // 添加
     AddImageRow(this.ds.Tables[0],this.gXJH.Text.Trim(),this.gZKZH.Text.Trim(),this.gKD.Text.Trim(),this.gXM.Text.Trim(),this.cbbXB.Text.Trim(),Application.StartupPath+"//Images//NoPhoto.jpg",Convert.ToInt32(this.gKCH.Text.Trim()),"",this.gZWH.Text.Trim(),this.dtpKSSJ.Value.ToString().Trim(),this.dtpJSSJ.ToString().Trim());
    }
    else
    {
     string str="select j.xjh,s.zkzh,j.xxdm,j.xm,j.xb,j.zp,s.kch,s.kc,s.zwh,k.kssj,k.jssj from(xs_ksxx s inner join xs_jbxx j on s.xjh=j.xjh) inner join ks_kcsj k on s.ksh=k.ksh and s.kc=k.kc where s.xjh='"+this.gXJH.Text.Trim()+"'";
     ldb.Exec4DS(str,"zkz",out ds);
     ((CrystalDecisions.CrystalReports.Engine.TextObject)crenp.ReportDefinition.ReportObjects["TxtTitle"]).Text="打印考生"+ds.Tables[0].Rows[0]["xm"].ToString().Trim()+"的准考证"; 
    }
    crenp.SetDataSource(this.ds);
    crenp.OpenSubreport("CRcg").SetDataSource(this.ds.Tables[0].Rows[0]["zp"]);//子报表CRcg显示图片
    crenp.OpenSubreport("CRzkz").SetDataSource(this.ds.Tables[0].Rows[0]["zp"]);子报表CRzkz
    crenp.Refresh();
    FmExamNoPrint enp=new FmExamNoPrint();
    enp.Show();//显示报表窗体
   }
   catch(Exception ex)
   {
    MessageBox.Show(ex.ToString());
   }
  } 

  /// <summary>
  /// 添加图片
  /// </summary>
  /// <param name="tbl"></param>
  /// <param name="name"></param>
  /// <param name="filename"></param>
  private void AddImageRow(DataTable tbl,string xjh,string zkzh,string xxdm,string xm,string xb,string filename,int kch,string kc,string zwh,string kssj,string jssj)
  {
   FileStream fs = new FileStream(filename, FileMode.Open); // 创建文件流
   BinaryReader br = new BinaryReader(fs);    // 创建二进制读取器
   DataRow row;
   // 创建一个新的数据行
   row = tbl.NewRow();

   // 设置 country 字段和 image 字段
   row["xjh"] = xjh;
   row["zkzh"] = zkzh;
   row["xxdm"] = xxdm;
   row["xm"] = xm;
   row["xb"] = xb;
   row["zp"] = br.ReadBytes((int)br.BaseStream.Length);
   row["kch"] = kch;
   row["kc"] = kc;
   row["zwh"] = zwh;
   row["kssj"] = kssj;
   row["jssj"]=jssj;

   // 将数据行添加到表中
   tbl.Rows.Add(row);

   // 清除
   br = null;
   fs = null;
  }

原创粉丝点击