主要后台逻辑:
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
namespace firstWpf
{
/// <summary>
/// Window1.xaml 的交互逻辑
/// </summary>
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
//生成报表
private void ReportViewer_Load()
{
Microsoft.Reporting.WinForms.ReportDataSource reportDataSource1 = new Microsoft.Reporting.WinForms.ReportDataSource();
this.reportViewer2.LocalReport.DataSources.Clear();
firstWpfDataSet dataset = new firstWpfDataSet();
string txt = this.datepicker1.Text;
//查询数据库
DataTable dt_data = ConnectionSQLServerFunc();
//var li=(from a in dt_data.AsEnumerable()
// group a by a.Field<DateTime>("time") into g
// // where g.Count() > 1
// select new
// {
// ph_avg = g.Average(x => x.Field<double>("PH")),
// bp_avg = g.Average(x => x.Field<double>("BP")),
// ap_avg = g.Average(x => x.Field<double>("AP")),
// time = g.Key
// }).ToList();
List<area_rdlc> list = DataTableToList<area_rdlc>(dt_data);
List<area_rdlc> all_list = new List<area_rdlc>() { };
//计算日期
List<string> timeArr= CalTime(txt);
//组装数据
for (int i = 0; i < timeArr.Count(); i++)
{
all_list.Add(new area_rdlc
{
ID = i,
PH = 0,
BP = 0,
AP=0,
time=DateTime.Parse(timeArr[i])
});
}
//最终数据
var result = (from a in list
group a by a.time into g
// where g.Count() > 1
select new {
ph_avg= g.Average(x => x.PH),
bp_avg= g.Average(x => x.BP),
ap_avg= g.Average(x => x.AP),
time=g.Key
} ).ToList();
for (int i = 0; i < result.Count; i++)
{
for (int j = 0; j < all_list.Count; j++)
{
if (result[i].time.Equals(all_list[j].time))
{
all_list[j].PH = result[i].ph_avg;
all_list[j].BP = result[i].bp_avg;
all_list[j].AP = result[i].ap_avg;
}
}
}
//绑定数据
dataset.BeginInit();
reportDataSource1.Name = "DataSet1";
reportDataSource1.Value = all_list;
this.reportViewer2.LocalReport.DataSources.Add(reportDataSource1);
this.reportViewer2.LocalReport.ReportEmbeddedResource = "firstWpf.Report2.rdlc";
dataset.EndInit();
//传递参数
ReportParameter[] rp = new ReportParameter[1];
rp[0]=new ReportParameter("time", txt);
this.reportViewer2.LocalReport.SetParameters(rp);
reportViewer2.ShowParameterPrompts = false;
reportViewer2.RefreshReport();
}
//根据年月获取所有日期
public List<string> CalTime(string txt) {
DateTime beginTime=DateTime.Parse(txt+"-01");//本月初
DateTime endTime=DateTime.Parse( beginTime.AddMonths(1).AddDays(-1).ToShortDateString());//本月最后一天
List<string> de = new List<string>();
for (DateTime dt = beginTime; dt <= endTime; dt = dt.AddDays(1))
{
de.Add(dt.ToShortDateString());
}
return de;
}
//取数据库数据
public DataTable ConnectionSQLServerFunc()
{
//连接数据库字符串
string strConn = "Data Source=.;Initial Catalog=firstWpf;User ID=sa;Password=123456";
DataTable dt = new DataTable();
using (SqlConnection conn = new SqlConnection(strConn))
{
try
{
string time = this.datepicker1.Text;
string[] timeStr = time.Split('-');
//连接数据库
conn.Open();
//查询数据库语句
string commandStr = string.Format("select * from Area_Rdlc where Year(time)='{0}' AND Month(time)='{1}'", timeStr[0], timeStr[1]);
//要对数据源执行的 SQL 语句或存储过程
SqlCommand sqlCmd = new SqlCommand(commandStr, conn);
//表示一组数据命令和一个数据库连接,它们用于填充 System.Data.DataSet 和更新数据源。
SqlDataAdapter sqlDataAda = new SqlDataAdapter(sqlCmd);
//数据的内存中缓存
//将获取到的数据填充到数据缓存中
sqlDataAda.Fill(dt);
}
catch (Exception ex)
{
}
}
return dt;
}
/// <summary>
/// 利用反射将DataTable转换为List<T>对象
/// </summary>
/// <param name="dt">DataTable 对象</param>
/// <returns>List<T>集合</returns>
public static List<T> DataTableToList<T>(DataTable dt) where T : class, new()
{
// 定义集合
List<T> ts = new List<T>();
//定义一个临时变量
string tempName = string.Empty;
//遍历DataTable中所有的数据行
foreach (DataRow dr in dt.Rows)
{
T t = new T();
// 获得此模型的公共属性
PropertyInfo[] propertys = t.GetType().GetProperties();
//遍历该对象的所有属性
foreach (PropertyInfo pi in propertys)
{
tempName = pi.Name;//将属性名称赋值给临时变量
//检查DataTable是否包含此列(列名==对象的属性名)
if (dt.Columns.Contains(tempName))
{
//取值
object value = dr[tempName];
//如果非空,则赋给对象的属性
if (value != DBNull.Value)
{
pi.SetValue(t, value, null);
}
}
}
//对象添加到泛型集合中
ts.Add(t);
}
return ts;
}
private void button_Click(object sender, RoutedEventArgs e)
{
//查询并生成报表
ReportViewer_Load();
}
}
}
页面截图:
源码下载:https://download.csdn.net/download/qq_34017733/11141215