水晶报表动态加载图片(签名)

 

最近弄水晶签名显示时,网上搜索了下,关于数据库保存路径动态显示图片的方法,感谢

http://blog.csdn.net/david_520042/archive/2009/10/15/4674140.aspx

 

水晶报表动态控制图片显示 Changing pictures dynamically in Crystal Report
项目需要在报表中根据条件动态显示不同的签名
查找了好多解决方案,一个比较简单的方案:

http://www.cnblogs.com/kingjiong/archive/2009/01/07/1370874.html
本人试验是可以用的,但是正如作者所讲,基于CR XI 独立版本实现。CR9和CR10都无此功能。
就是说,用独立CR XI 完整版本做,可以实现效果,但是一旦在VS2005 项目中使用,这个效果是出不来的.

后来在网络上找到幻想天空文章

http://hi.baidu.com/awming520/blog/item/3a6ccc2ab082a924d52af11f.html
水晶报表动态加载图片分为数据库图片和本地图片。

首先创建水晶报表rpt文件,方法还是采用xsd数据集的方式,简便容易操作,记得将数据集中的相关字段设为你想要在报表中呈现的字段。比如说在内容左边加入一个图片,就将相应的图片字段拖放过去。能不能直接在水晶报表上添加一个图片然后修改?我做了几次实验,好像都不得要领,所以还是放弃,走最老式的方式,先在数据库中创建一个准备在水晶报表中使用到的字段的表,然后再通过这张表创建数据集xsd文件,再通过数据集创建水晶报表。虽然方法绕了路,但是绝对是可行的。

通过数据集创建dataset

            DataSet1.pictureTabDataTable pt = new DataSet1.pictureTabDataTable();
            DataSet1TableAdapters.pictureTabTableAdapter da =
                new DataSet1TableAdapters.pictureTabTableAdapter();
            da.Fill(pt);
            CrystalReport1 cr = new CrystalReport1();
            cr.SetDataSource((DataTable)pt);
            this.crystalReportViewer1.ReportSource = cr;

表名叫做pictureTab,因此创建的数据集有表pictureTabDataTable类和适配器pictureTabTableAdapter,奇怪的是前面的命名空间,着实没有找到,不过无关要害。

通过数据库创建Dataset

            SqlConnection conn = new SqlConnection("Data Source=(Local);Initial Catalog=MyDB;User ID=sa;Password=yamato");
            SqlDataAdapter da = new SqlDataAdapter("select * from pictureTab where name ='e'", conn);
            DataSet ds = new DataSet();
            da.Fill(ds, "NewTable");
            CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            doc.Load(@"C:\Documents and Settings\jiez\桌面\CrystalReportsApplication1\CrystalReportsApplication1\CrystalReport1.rpt");
            doc.SetDataSource(ds.Tables["NewTable"]);
            this.crystalReportViewer1.ReportSource = doc;

这个其实也没什么好说的,只是修改了一下报表加载的方式而已。

厉害的来了,本地图片加载

            DataSet ds = new DataSet();
            ds.Tables.Add("NewTable");
            ds.Tables[0].Columns.Add("id", Type.GetType("System.Int32"));
            ds.Tables[0].Columns.Add("picture", Type.GetType("System.Byte[]"));
            ds.Tables[0].Columns.Add("name", Type.GetType("System.String"));
            string picPath = @"C:\Documents and Settings\jiez\My Documents\My Pictures\logo.jpg";
            AddOneRow(ds.Tables["NewTable"], "1", picPath, "Jackey");
            CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
            doc.Load(@"C:\Documents and Settings\jiez\桌面\CrystalReportsApplication1\CrystalReportsApplication1\CrystalReport1.rpt");
            doc.SetDataSource(ds.Tables["NewTable"]);
            this.crystalReportViewer1.ReportSource = doc;

        public void AddOneRow(DataTable tbl, string c1, string c2, string c3)
        {
            FileStream fs = new FileStream(c2, FileMode.Open);
            BinaryReader br = new BinaryReader(fs);
            DataRow row = tbl.NewRow();
            row[0] = c1;
            row[2] = c3;
            row[1] = br.ReadBytes((int)br.BaseStream.Length);
            tbl.Rows.Add(row);

        }

这里自己创建了一个dataset,并且往里面添加了数据,都是自己进行设定,这样灵活性就很高了,要注意的是这里的数据读取采用的是BinaryReader直接就用二进制的方式进行了读取。
是可以实现的.
注释:1.AddOneRow 函数中并没有关闭数据流 fs1.Close();fs2.Close();
    public void AddOneRow(DataTable tbl, string c1, string c2, string c3)
    {
        FileStream fs1 = new FileStream(c2, FileMode.Open);
        FileStream fs2 = new FileStream(c3, FileMode.Open);
        BinaryReader br1 = new BinaryReader(fs1);
        BinaryReader br2 = new BinaryReader(fs2);

        DataRow row = tbl.NewRow();
        row[1] = br1.ReadBytes((int)br1.BaseStream.Length);
        row[2] = br2.ReadBytes((int)br2.BaseStream.Length);
        tbl.Rows.Add(row);

        fs1.Close();
        fs2.Close();

    }
    protected void Page_Load(object sender, EventArgs e)
    {
         //sql
         SqlConnection conn = new SqlConnection(PublicVar.strCon_SHATDB);
         SqlDataAdapter da = new SqlDataAdapter("select * from stamp ", conn);
         DataSet ds = new DataSet();
         da.Fill(ds, "NewTable");
         //存储过程取得ds
         SqlParameter[] parms = new SqlParameter[1];
         parms[0] = new SqlParameter("@id", SqlDbType.Int);
         parms[0].Value = 2; //部门
         DataSet ds = new DataSet();
         ds = DataAccess.DataAccessSHDB.GetTableByStore("test3", parms).DataSet;

         //DataSet ds = new DataSet();

         string picPath1 = @"C:\test\1.jpg";
         string picPath2 = @"C:\test\2.jpg";
         AddOneRow(ds.Tables[0], "1", picPath1, picPath2);
         CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
         doc.Load(@"C:\test\1.rpt");//报表已经设置签名的image字段
         doc.SetDataSource(ds.Tables[0]);
         CrystalReportViewer1.ReportSource = doc;
         CrystalReportViewer1.DataBind();
    }
后来我根据思路重构了一下
报表的dataset中设置需要签名的字段一定要是IMAGE 路径保存在数据中
    public void AddOneRow(DataTable tbl, string c1, string c2, string c3, string c4, string c5,string c6,string c7)
    {
        //FileStream fs1 = new FileStream(c2, FileMode.Open);
        //FileStream fs2 = new FileStream(c3, FileMode.Open);
        //BinaryReader br1 = new BinaryReader(fs1);
        //BinaryReader br2 = new BinaryReader(fs2);
        //DataRow row = tbl.NewRow();
        //row[0] = c1;
        //row[0] = br1.ReadBytes((int)br1.BaseStream.Length);
        //row[1] = br2.ReadBytes((int)br2.BaseStream.Length);
        //fs1.Close();
        //fs2.Close();
        //tbl.Rows.Add(row);
        for (int i = 0; i < tbl.Rows.Count; i++) //如果在头部那么第一行就够了 如果在尾部最后一行就OK 如果在Group 中就必须所有行
        {

            FileStream fs1 = new FileStream(c1, FileMode.Open);
            FileStream fs2 = new FileStream(c2, FileMode.Open);
            FileStream fs3 = new FileStream(c3, FileMode.Open);
            FileStream fs4 = new FileStream(c4, FileMode.Open);
            FileStream fs5 = new FileStream(c5, FileMode.Open);
            FileStream fs6 = new FileStream(c6, FileMode.Open);
            FileStream fs7 = new FileStream(c7, FileMode.Open);

            BinaryReader br1 = new BinaryReader(fs1);
            BinaryReader br2 = new BinaryReader(fs2);
            BinaryReader br3 = new BinaryReader(fs3);
            BinaryReader br4 = new BinaryReader(fs4);
            BinaryReader br5 = new BinaryReader(fs5);
            BinaryReader br6 = new BinaryReader(fs6);
            BinaryReader br7 = new BinaryReader(fs7);

            tbl.Rows[i][0] = br1.ReadBytes((int)br1.BaseStream.Length);
            tbl.Rows[i][1] = br2.ReadBytes((int)br2.BaseStream.Length);

            tbl.Rows[i][2] = br3.ReadBytes((int)br3.BaseStream.Length);
            tbl.Rows[i][3] = br4.ReadBytes((int)br4.BaseStream.Length);

            tbl.Rows[i][4] = br5.ReadBytes((int)br5.BaseStream.Length);
            tbl.Rows[i][5] = br6.ReadBytes((int)br6.BaseStream.Length);

            tbl.Rows[i][6] = br7.ReadBytes((int)br7.BaseStream.Length);
            fs1.Close();
            fs2.Close();
            fs3.Close();
            fs4.Close();
            fs5.Close();
            fs6.Close();
            fs7.Close();
            //tbl.Rows.Add(row);
        }
    }

    private void CrystalReportDataBind(int id)
    {
        SqlParameter[] parms = new SqlParameter[1];
        parms[0] = new SqlParameter("@id", SqlDbType.Int);
        parms[0].Value = id; //部门
        DataSet ds = new DataSet();
        ds = DataAccess.DataAccessSHDB.GetTableByStore("project", parms).DataSet;
        //以下部分是读取本地图片
        string strURL ="select u.stamp_path from taskline tl left join users u on tl.UserID=u.[ID] where taskcode='"+lbl_taskcode.Text+"' and TurnTo='1' AND Result='Y' ";
        string[] picPath=new string[7];
        for(int i=0;i<=6;i++)
        {
            if (string.IsNullOrEmpty(PublicFunction.StringGetValue(strURL + " and taskline=" + (i + 1), 0)) == false) //取得已经审批人的本地签名地址
            {
                //picPath[i] = "@" + PublicFunction.StringGetValue(strURL + " and taskline=" + (i + 1), 0);
                //string urlPath = Server.MapPath("..\\Reports\\"+PublicFunction.StringGetValue(strURL + " and taskline=" + (i + 1), 0));//无法动态解析服务器地址
                picPath[i] = PublicFunction.StringGetValue(strURL + " and taskline=" + (i + 1), 0); //保存本地地址
            }
            else
            {
                switch (i)
                {
                    case 0:
                       picPath[i] = @"C:\stamp\空白1.jpg";  //空白地址页面 防止进程同时被占用
                       break;
                   case 1:
                       picPath[i] = @"C:\stamp\空白2.jpg";
                       break;
                   case 2:
                       picPath[i] = @"C:\stamp\空白3.jpg";
                       break;
                   case 3:
                       picPath[i] = @"C:\stamp\空白4.jpg";
                       break;
                   case 4:
                       picPath[i] = @"C:\stamp\空白5.jpg";
                       break;
                   case 5:
                       picPath[i] = @"C:\stamp\空白6.jpg";
                       break;
                   case 6:
                       picPath[i] = @"C:\stamp\空白7.jpg";
                       break;
                }
            }
        }

        //string picPath1 = @"C:\test\2.jpg";
        //string picPath2 = @"C:\test\2.jpg";

        AddOneRow(ds.Tables[0], picPath[0], picPath[1], picPath[2], picPath[3], picPath[4], picPath[5], picPath[6]);
        CrystalDecisions.CrystalReports.Engine.ReportDocument doc = new CrystalDecisions.CrystalReports.Engine.ReportDocument();
        string reportPath = Server.MapPath("..\\Reports\\project.rpt");
        //doc.Load(@"C:\test\1.rpt");
        doc.Load(reportPath);
        doc.SetDataSource(ds.Tables[0]);
        CrystalReportViewer1.ReportSource = doc;
        CrystalReportViewer1.DataBind();
    }

 

转载于:https://www.cnblogs.com/hijack/archive/2010/05/11/1732429.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值