水晶报表 (悬赏100)

wenjun19781215 2009-04-01 09:23:30
我现在想用水晶报表打印数据库中的内容,文字可以打印出来但图片不行,我数据库中存的是图片的路径,现在请高人帮忙解决,如何在水晶报表中显示图片,这是现在使用的代码:(我用的数据库是access),我要具体的做法谢谢。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;
using CrystalDecisions.CrystalReports.Engine;
using System.Web.Caching;

namespace chc
{
public partial class PrintForm : Form
{
public PrintForm()
{
//InitializeComponent();
}

private void PrintForm_Load(object sender, EventArgs e)
{

DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection();
SqlDataAdapter da = new SqlDataAdapter("select * from FirstTable", cn);
SqlCommandBuilder bd = new SqlCommandBuilder(da);
da.Fill(ds);
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
DataSet ds1 = new DataSet();

if (ds.Tables[0].Rows[@"图片"] != null)
{
string path = Microsoft.SqlServer.Server.MapPath(ds.Tables[0].Rows["图片"].ToString());
if (File.Exists(path))
{
ds1.Tables.Add("student");
FileStream fs = new FileStream(path, FileMode.Open);
BinaryReader br = new BinaryReader(fs);
addrow(ds, ds1, i, br);
fillimg(ds1);
}
}

}
}
/// <summary>
/// 把文件找到读成二进制,然后调用addrow方法
/// </summary>
/// <param name="dssafe"></param>
/// <param name="ds"></param>
/// <param name="i"></param>
/// <param name="br"></param>
public void addrow(DataSet dssafe, DataSet ds, int i,BinaryReader br)
{
ds.Tables[0].Columns.Add("编号", typeof(System.String ));
ds.Tables[0].Columns.Add("品名", typeof(System.String));
ds.Tables[0].Columns.Add("图片", typeof(System.Byte[]));
ds.Tables[0].Columns.Add("长", typeof(System.Int32));
ds.Tables[0].Columns.Add("宽", typeof(System.Int32));
ds.Tables[0].Columns.Add("高", typeof(System.Int32));
ds.Tables[0].Columns.Add("数量", typeof(System.Int32));
ds.Tables[0].Columns.Add("单位", typeof(System.String));
ds.Tables[0].Columns.Add("设备相关性能及参数", typeof(System.String));
ds.Tables[0].Columns.Add("单价", typeof(System.Int32));
ds.Tables[0].Columns.Add("金额", typeof(System.Int32));
DataRow row = ds.Tables[0].NewRow();
row["编号"] = dssafe.Tables[0].Rows["编号"].ToString();
row["品名"] = dssafe.Tables[0].Rows["品名"].ToString();
row["图片"] = dssafe.Tables[0].Rows["图片"].ToString();
row["长"] = dssafe.Tables[0].Rows["长"].Int32();
row["宽"] = dssafe.Tables[0].Rows["宽"].Int32();
row["高"] = dssafe.Tables[0].Rows["高"].Int32();
row["数量"] = dssafe.Tables[0].Rows["数量"].Int32();
row["单位"] = dssafe.Tables[0].Rows["单位"].ToString();
row["设备相关性能及参数"] = dssafe.Tables[0].Rows["设备相关性能及参数"].ToString();
row["单价"] = dssafe.Tables[0].Rows["单价"].Int32();
row["金额"] = dssafe.Tables[0].Rows["金额"].Int32();
ds.Tables[0].Rows.Add(row);
}

/// <summary>
/// 把数据填充到新的DATASET里,即ds,然后把ds设置为报表的数据源
/// </summary>
/// <param name="ds"></param>
public void fillimg(DataSet ds)
{
if (Cache["dataset"] == null)
{
ds.ReadXmlSchema(Microsoft.SqlServer.Server.MapPath("dataTable.xsd"));
Cache["dataset"] = ds;
ReportDocument rp = new ReportDocument();
rp.Load(Microsoft.SqlServer.Server.MapPath("CrystalReport1.rpt"));
rp.SetDataSource(ds.Tables[0]);
CrystalReportViewer1.ReportSource = rp;
}
else
{
ds = (DataSet)Cache["dataset"];
ReportDocument rp = new ReportDocument();
rp.Load(Microsoft.SqlServer.Server.MapPath("CrystalReport1.rpt"));
rp.SetDataSource(ds.Tables[0]);
CrystalReportViewer1.ReportSource = rp;

}
}

}
}

...全文
101 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
jietuan 2009-04-01
  • 打赏
  • 举报
回复
你在水晶报表设计阶段用图片控件,在代码中获取该图片控件,改变它的图片URL,而且你的图片是直接存储在硬盘里面,更好了。


ReportDocument report = this.CrystalReportSource1.ReportDocument;
TextObject obj=report.ReportDefinition.Sections["Section3"].ReportObjects["Text3"] as TextObject;

你可以获取图片控件,采用同样的方式
贫僧又回来了 2009-04-01
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 hostbb 的回复:]
我没有找到,而且我查了,不能显示从数据库动态显示图片。如果做过请贴出相关代码。谢谢
[/Quote]
控件是有的,但好象只能以图片的路径赋值,具体没做过!帮顶
zhensoft163 2009-04-01
  • 打赏
  • 举报
回复
我们当初做的是将图片直接放上去,固定的图片
wenjun19781215 2009-04-01
  • 打赏
  • 举报
回复
我没有找到,而且我查了,不能显示从数据库动态显示图片。如果做过请贴出相关代码。谢谢
linaren 2009-04-01
  • 打赏
  • 举报
回复
水晶报表中应该有图片控件的吧,直接拖上去或动态生成赋给

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧