新闻详情
FastReport.Net报表工具表格的使用:如何动态展示列和填充单元格——考勤报表
FastReport.Net报表工具表格的使用:如何动态展示列和填充单元格——考勤报表
起因是我在使用FastReport.Net工具的时候搜不到对于我的问题的具体解决办法所以开始写csdn来记录自己当前遇到的问题。有关FastReport.Net的问题欢迎一起讨论。目标这是一个考勤报表展示每个员工当前年月的出勤状态、天数。要求1.第一行展示第二行日期对应的星期几2、根据月份动态展示列。下面这张图是我已经画好的整个图接下来就只需要在脚本即code里面写完整的代码。//这个方法可以在右边属性栏找到闪电图标会有这个事件ManualBuild双击进去//Table2就是你给这个Table控件命的名称privatevoidTable2_ManualBuild(objectsender,EventArgse){TableObjecttablesenderasTableObject;if(tablenull)return;// 1. 获取主表数据源用于判断年月DataSourceBasemainSourceReport.GetDataSource(V_HR_SimpleTimeSheetInfoList);// 2.获取子表数据源填充数据行DataSourceBaserowDataReport.GetDataSource(V_HR_SimpleTimeSheet);if(mainSourcenull)return;// 移动到第一行获取年月假设主表只有一条记录或取第一条mainSource.First();intyearConvert.ToInt32(mainSource[TSYear]);intmonthConvert.ToInt32(mainSource[TSMonth]);// 2. 计算该月最大天数intmaxDaysDateTime.DaysInMonth(year,month);// 3. 定义日期列的起始索引// 假设第2~31列对应 Day1~Day31// 请根据你的实际设计器列顺序修改这个 startColIndexintstartColIndex2;//这个是因为我第一行需要填充的星期的开始名称为Cell169intstartWeekCellIndex169;// 4. 循环处理 1 到 31 号for(inti1;i31;i){// 计算当前列对应的 Cell 变量名索引// 偏移量是 i-1因为数组/索引是从 0 开始的intcurrentWeekIndexstartWeekCellIndex(i-1);// 尝试通过反射或 FindObject 获取单元格对象// 注意这里使用 FindObject 是最稳妥的因为它能处理动态生成的名称stringweekCellNameCellcurrentWeekIndex.ToString();BaseweekCellReport.FindObject(weekCellName)asBase;// 如果找到了单元格对象if(weekCell!null){if(imaxDays){// --- 情况 A: 该日期存在 (例如 6月1日 - 6月30日) ---// 1. 构造日期对象DateTimecurrentDaynewDateTime(year,month,i);// 2. 填充内容// 设置星期 (例如 Mon, Tue)这里因为我要显示英文如果不显示可以删除System.Globalization.CultureInfo.InvariantCulture((TextObject)weekCell).TextcurrentDay.ToString(ddd,System.Globalization.CultureInfo.InvariantCulture);}else{((TextObject)weekCell).Text;}}}// 5. 循环隐藏多余列// 表格通常预置了31列数据列我们从 maxDays1 开始隐藏for(intimaxDays1;i31;i){// 计算当前要隐藏的列索引intcolIndexstartColIndex(i-1);// 安全检查防止索引越界if(colIndextable.Columns.Count){// 核心操作直接隐藏列// 只要 Visible falseFastReport 就不会渲染这一列也不会报错table.Columns[colIndex].Visiblefalse;}}//6. 分表头数据行打印表格// init the data sourcerowData.Init();// print the first table row - it is a headerTable2.PrintRow(0);// each PrintRow call must be followed by either PrintColumn or PrintColumns call// to print cells on the rowTable2.PrintColumns();// print the first table row - it is a headerTable2.PrintRow(1);// each PrintRow call must be followed by either PrintColumn or PrintColumns call// to print cells on the rowTable2.PrintColumns();// now enumerate the data source and print the table bodywhile(rowData.HasMoreRows){// print the table bodyTable2.PrintRow(2);Table2.PrintColumns();// go next data source rowrowData.Next();}}最终效果