最近在做一個報表,客戶堅持要報表實現行合併,使用了TextObject的Duplicates屬性,設置爲Merge。但是效果不太好,相鄰的相似就合併,顯得報表很雜亂,效果如下:
而客戶想要的效果是這樣的:
網上搜了一下關於fastreport單元格合併的文章,有說改源碼的,有說寫腳本的,還有人賣修改後的控件的,總之沒有找到現成的方法。所以在客戶的壓力下不得不自己想辦法解決,首先想着改源碼,可是沒有源碼啊!!!反編譯出來的又太雜亂,編譯錯誤一大堆,所以就改爲從腳本入手了。
其實要實現客戶這種效果,只要解決不同“層級”間即使內容相同也不合並就可以了。怎樣使不同“層級”的單元格,即使內容相同也不合並呢,這裏用Duplicates屬性設置爲Merge顯然不能實現了,所以我想到了之前做項目遇到過的不可見字符的問題。在ASCII碼中有很多特殊字符,比如“(char)0”,它就是一個不顯示的字符,如果相鄰“層級”的數據按奇偶行的模式分別在前面加上空字符串("")和不可見字符(((char)0).ToString()),豈不是在不同“層級”間就不合並了!
private void Text5_BeforePrint(object sender, EventArgs e)
{
DataSourceBase dsb= Report.GetDataSource("BOMLIST");
if(lasttext1==""||dsb["LAYER"].ToString()!=lasttext1)
{
lasttext1=dsb["LAYER"].ToString();
lastadd = lastadd==""?((char)0).ToString():"";
}
Text5.Text=lastadd+Text5.Text;
}
因爲不同“層級”間“層級”和“物料編號”不會相同,所以選擇最左邊會出現相同的“物料名稱”的BeforePrint事件來做這一動作。由於報表數據加載是從左到右、自上而下,所以後面的TextObject只需要把lastadd加上就可以了。
詳細代碼如下:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Data;
using FastReport;
using FastReport.Data;
using FastReport.Dialog;
using FastReport.Barcode;
using FastReport.Table;
using FastReport.Utils;
namespace FastReport
{
public class ReportScript
{
private string lasttext1="";
private string lastadd="";
private int loadcount=0;
private void Text9_BeforePrint(object sender, EventArgs e)
{
Text9.Text=lastadd+Text9.Text;
}
private void Text5_BeforePrint(object sender, EventArgs e)
{
DataSourceBase dsb= Report.GetDataSource("BOMLIST");
if(lasttext1==""||dsb["LAYER"].ToString()!=lasttext1)
{
lasttext1=dsb["LAYER"].ToString();
lastadd = lastadd==""?((char)0).ToString():"";
}
Text5.Text=lastadd+Text5.Text;
}
private void Text7_BeforePrint(object sender, EventArgs e)
{
Text7.Text=lastadd+Text7.Text;
}
private void Text19_BeforePrint(object sender, EventArgs e)
{
Text19.Text=lastadd+Text19.Text;
}
private void Text21_BeforePrint(object sender, EventArgs e)
{
Text21.Text=lastadd+Text21.Text;
}
private void Text23_BeforePrint(object sender, EventArgs e)
{
Text23.Text=lastadd+Text23.Text;
}
private void Text25_BeforePrint(object sender, EventArgs e)
{
Text25.Text=lastadd+Text25.Text;
}
private void Text27_BeforePrint(object sender, EventArgs e)
{
Text27.Text=lastadd+Text27.Text;
}
private void Text29_BeforePrint(object sender, EventArgs e)
{
Text29.Text=lastadd+Text29.Text;
}
private void Text31_BeforePrint(object sender, EventArgs e)
{
Text31.Text=lastadd+Text31.Text;
}
}
}
這樣就可以實現客戶想要的效果了。