RDLC报表动态显示一维条码的方法

using Microsoft.Reporting.WinForms;
using System;
using System.Data;
using System.Windows.Forms;

namespace 一维码
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ReportViewer reportViewer = new ReportViewer();//创建报表
private void button1_Click(object sender, EventArgs e)
{
reportViewer.ProcessingMode = ProcessingMode.Local;//设置本地处理模式
reportViewer.Dock = DockStyle.Fill;//设置报表控件填满窗体
this.groupBox1.Controls.Add(reportViewer);//填加报表控件到窗体
reportViewer.LocalReport.DataSources.Clear();//清空报表数据
reportViewer.LocalReport.ReportEmbeddedResource = "一维码.Report1.rdlc";//设置rdlc文件,ReportEmbeddedResource(嵌入式模式)
//reportViewer1.LocalReport.ReportPath = "Book.Report1.rdlc";
reportViewer.LocalReport.DataSources.Add(new ReportDataSource("DataSet1", LoadDepartmentsData()));//DataSet1是设计报表时指定的数据集名称
// reportViewer.Drillthrough += new DrillthroughEventHandler(DemoDrillthroughEventHandler);//添加钻取事件
reportViewer.LocalReport.Refresh();
reportViewer.RefreshReport();//刷新报表,这句一定要有
}
private DataTable LoadDepartmentsData()
{
DataSet dt = new DataSet();
string sql = "SELECT BookId FROM Book";
dt = SqlHelper.SqlHelper.GetDataSet(sql);
dt.Tables[0].Columns.Add("bytes", typeof(Byte[]));
for (int i = 0; i < dt.Tables[0].Rows.Count; i++)
{
dt.Tables[0].Rows[i]["bytes"] = ImageToBytes(dt.Tables[0].Rows[i][0].ToString());
}
return dt.Tables[0];
}
//image对象转byte数组
public static Byte[] ImageToBytes(string str)
{
//BarcodeLib.dll
BarcodeLib.Barcode b = new BarcodeLib.Barcode();
b.IncludeLabel = true;
b.RotateFlipType = 0;
//label.alignment and position
b.LabelPosition = BarcodeLib.LabelPositions.BOTTOMCENTER;
var type = BarcodeLib.TYPE.CODE128;
//Encoding performed here
b.Encode(type,str,300,40);
BarcodeLib.SaveTypes savetype = BarcodeLib.SaveTypes.UNSPECIFIED;
savetype = BarcodeLib.SaveTypes.PNG;
byte[] br = b.GetImageData(savetype);
return br;
}
}
}

=System.Convert.ToBase64String(Fields!bytes.Value)
image/jpeg

ReportParameter stuName = new ReportParameter("StuName", "张三");
        ReportParameter stuClass = new ReportParameter("stuClass", "计算机一班");
        this.reportViewer.LocalReport.SetParameters(new ReportParameter[] { stuName, stuClass });
posted @ 2020-09-14 23:11  zhujie-  阅读(797)  评论(0编辑  收藏  举报