C#WinForm中追加報表以及圖片的功能

效果圖片:工具

分步解析實現過程:this

一.新建報表所須要的文件以及類:3d

1.新建一個WindowForm項目WindowsFormsReport;orm

2. 添加Entity文件夾,並添加StudentEntity.cs類;對象

 public class StudentEntityblog

{圖片

/// <summary>
/// 姓名
/// </summary>
public string StuName { get; set; }
/// <summary>
/// 年齡
/// </summary>
public string StuAge { get; set; }
/// <summary>
/// 學生住址
/// </summary>
public string StuAddress { get; set; }
/// <summary>
/// 身高
/// </summary>
public string StuHeight { get; set; }
/// <summary>
/// 體重
/// </summary>
public string StuWeight { get; set; }
/// <summary>
/// 學生電話
/// </summary>
public string StuPhone { get; set; }
}get

3.添加Report文件夾,並添加Report.rdlc報表;string

建好報表,打開報表,點擊界面上的數據集右擊,新建數據集,就會彈出以下界面:it

點擊數據源類型,這裏咱們以對象爲例:

接下來,咱們只需完成後,再點擊肯定就能夠,在報表界面進行追加列表:

往界面上追加圖片,以下操做:

1)點擊界面上的參數,右擊新建參數:

2)在界面上右擊新插入圖片,則會彈出對圖片的配置界面以下:

此處須要注意:圖片的格式

3)對使用字段設置,以下圖參考便可:

=System.Convert.FromBase64String(Parameters!ReportParameter1.Value)

這樣如上面的圖所設置便可:

二.如今對WindowForm頁面設置:

1)選擇工具箱中報表-ReportViewer控件,拖入界面中

2)上面的圖,畫黃色的位置,選擇上面新建的報表:

3)用代碼實現展現:這裏我用畫面啓動就加載數據:


public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

this.reportViewer1.RefreshReport();
}

private void reportViewer1_Load(object sender, EventArgs e)
{
reportViewer1.LocalReport.DataSources.Clear();

// 插入圖片設置
reportViewer1.LocalReport.EnableExternalImages = true;
Bitmap bmp = new Bitmap(Image.FromFile("E:\\C#Del\\Lianxi\\WindowsFormsReport\\QQ.jpg"));
MemoryStream msm = new MemoryStream();
bmp.Save(msm, ImageFormat.Jpeg);
byte[] bytes = msm.ToArray();

ReportParameter repter = new ReportParameter("ReportParameter1",Convert.ToBase64String(bytes));
reportViewer1.LocalReport.SetParameters(new ReportParameter[] { repter });

reportViewer1.LocalReport.DataSources.Add(new Microsoft.Reporting.WinForms.ReportDataSource("DataSet1",DataList()));
reportViewer1.RefreshReport();
}

private List<StudentEntity> DataList()
{
List<StudentEntity> wStudentEntityList = new List<StudentEntity>();
StudentEntity wStudentEntity = new StudentEntity();
wStudentEntity.StuName = "離散";
wStudentEntity.StuAddress = "十里堡10號";
wStudentEntity.StuAge = "23歲";
wStudentEntity.StuHeight = "169cm";
wStudentEntity.StuWeight = "69Kg";
wStudentEntity.StuPhone = "13011108121";
wStudentEntityList.Add(wStudentEntity);
return wStudentEntityList;
}

接下來,咱們啓動下面,效果以下:

注意:若你們有疑問,能夠給我留言,你們一塊兒交流

相關文章
相關標籤/搜索