效果圖片: 分步解析實現過程: 一.新建報表所需要的文件以及類: 1.新建一個WindowForm項目WindowsFormsReport; 2. 添加Entity文件夾,並添加StudentEntity.cs類; public class StudentEntity { /// <summary> ...
效果圖片:
分步解析實現過程:
一.新建報表所需要的文件以及類:
1.新建一個WindowForm項目WindowsFormsReport;
2. 添加Entity文件夾,並添加StudentEntity.cs類;
public class StudentEntity
{
/// <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; }
}
3.添加Report文件夾,並添加Report.rdlc報表;
建好報表,打開報表,點擊界面上的數據集右擊,新建數據集,就會彈出如下界面:
點擊數據源類型,這裡我們以對象為例:
接下來,我們只需完成後,再點擊確定就可以,在報表界面進行追加列表:
往界面上追加圖片,如下操作:
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;
}
接下來,我們啟動下麵,效果如下:
註意:若大家有疑問,可以給我留言,大家一起交流