1.1.根据DevExpress的bandGridView表头生成打印表格表头(递归 在树结构中的应用)
private void button1_Click(object sender, EventArgs e)
{
DevExpress.XtraReports.UI.XtraReport report = new DevExpress.XtraReports.UI.XtraReport();
report.LoadLayout(".\\xtraReport1.repx");
DevExpress.XtraReports.UI.XRTable table1 = report.FindControl("table1", true) as XRTable;
DevExpress.XtraReports.UI.XRTable table= new XRTable(); //
DevExpress.XtraReports.UI.DetailBand detail = report.FindControl("detailband1", true) as DetailBand;
table.WidthF = 800;
iTreeHeight = 0;
jLeaves = 0;
iheight = 1;
iTreeLevel=1;
iwidth = 0;
GetTreeHeightWidth(this.bandedGridView1.Bands);
for (int r = 0; r < iTreeLevel; r++)
{
DevExpress.XtraReports.UI.XRTableRow row = new XRTableRow();
row.HeightF=25;
row.WidthF=800;
DevExpress.XtraReports.UI.XRTableCell cell;
for (int c = 0; c < iwidth; c++)
{
cell = new XRTableCell();
cell.Text = "";
cell.WidthF = 800 / iwidth;
row.Cells.Add(cell);
}
table.Rows.Add(row);
table.HeightF = table.HeightF + 25;
}
table.Borders = DevExpress.XtraPrinting.BorderSide.All;
table.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
BuildPrintHeadAndCol2(table, this.bandedGridView1.Bands, null);
table.Parent = detail;
report.ShowDesigner();
}
int iheight = 1,
iTreeLevel=1, //树的高度,以最高深度为准
iwidth = 0; //树的宽度,叶子树
/// <summary>
/// 待测量的树,功能是测量树高和叶子树
/// </summary>
/// <param name="collection"></param>
private void GetTreeHeightWidth(DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection collection)
{
foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand band in collection)
{
if (band.HasChildren) //树枝
{
iheight++; //树枝代表高度,往下测量
GetTreeHeightWidth(band.Children); //递归子树
}
else
{
iwidth++; //树叶
}
}
if (iheight > iTreeLevel)
{
iTreeLevel = iheight; //最高的为树的高度
}
iheight--; //往上测量
}
int iTreeHeight = 0, //树高
jLeaves = 0;//树叶
List<string[]> lst = new List<string[]>(); //第一个是高度i,第二个是宽度j,这里的宽度是之前的叶子数,第三个是标题text
private void BuildPrintHeadAndCol2(DevExpress.XtraReports.UI.XRTable tb,
DevExpress.XtraGrid.Views.BandedGrid.GridBandCollection collection, DevExpress.XtraGrid.Views.BandedGrid.GridBand parent)
{
foreach (DevExpress.XtraGrid.Views.BandedGrid.GridBand band in collection)
{
if (band.HasChildren) //树枝
{
lst.Add(new string[] { iTreeHeight.ToString(), jLeaves.ToString(), band.Caption });
iTreeHeight++;
BuildPrintHeadAndCol2(tb,band.Children,band);
}
else //叶节点,处理的是上下合并
{
int rowIndex = iTreeLevel / 2 - 1; //当前节点所在的位置
if (parent ==null) //没有父节点
{
if (iTreeLevel % 2 != 0)
{
//tb.Rows[rowIndex].Cells[jLeaves].Text = tb.Rows[iTreeLevel / 2 + 1].Cells[jLeaves].Text = "";
tb.Rows[rowIndex + 1].Cells[jLeaves].Text = band.Caption;
tb.Rows[rowIndex + 1].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right;
tb.Rows[rowIndex].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Top;
tb.Rows[rowIndex + 2].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;
}
else
{
tb.Rows[rowIndex].Cells[jLeaves].Text = band.Caption;
tb.Rows[rowIndex].Cells[jLeaves].TextAlignment = DevExpress.XtraPrinting.TextAlignment.BottomCenter;
tb.Rows[rowIndex].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Top;
//tb.Rows[rowIndex + 1].Cells[jLeaves].Text = "";
tb.Rows[rowIndex + 1].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom;
}
}
else
{
if (iTreeLevel - iTreeHeight == 1)
{
tb.Rows[iTreeLevel - 1].Cells[jLeaves].Text = band.Caption;
}
else
{
if ((iTreeLevel - iTreeHeight) % 2 == 0) //刚好除尽的话
{
tb.Rows[iTreeHeight + rowIndex].Cells[jLeaves].Text = band.Caption;
tb.Rows[iTreeHeight + rowIndex].Cells[jLeaves].TextAlignment = DevExpress.XtraPrinting.TextAlignment.BottomCenter;
tb.Rows[iTreeHeight + rowIndex].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Top;
tb.Rows[iTreeHeight + rowIndex + 1].Cells[jLeaves].Text = "";
}
else
{
//tb.Rows[iTreeHeight + (iTreeLevel - iTreeHeight) / 2 - 2].Cells[jLeaves].Text = tb.Rows[iTreeHeight + (iTreeLevel - iTreeHeight) / 2].Cells[jLeaves].Text = "";
tb.Rows[iTreeHeight + (iTreeLevel - iTreeHeight) / 2 - 1].Cells[jLeaves].Text = band.Caption;
tb.Rows[iTreeHeight + (iTreeLevel - iTreeHeight) / 2 - 1].Cells[jLeaves].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right;
}
}
}
jLeaves++;
}
}
if (lst.Count>0) //非叶子的树枝,处理的是左右
{
int icout = lst.Count - 1;
int rindex = Convert.ToInt32(lst[icout][0]);
int cindex = Convert.ToInt32(lst[icout][1]);
int cellindex = cindex + (jLeaves - cindex) / 2 - 1; //文本所在表格位置
string caption = lst[icout][2];
if ((jLeaves - cindex) % 2 != 0)
{
cellindex = cellindex + 1; //中间
}
else
{
tb.Rows[rindex].Cells[cellindex].TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
}
for (int cellcount = cindex; cellcount < jLeaves; cellcount++)
{
if (cellcount == cellindex)
{
tb.Rows[rindex].Cells[cellcount].Text = lst[icout][2];
}
if (cellcount == cindex) //第一个表格
{
tb.Rows[rindex].Cells[cellcount].Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
}
else if (cellcount == jLeaves - 1) //最后一个表格
{
tb.Rows[rindex].Cells[cellcount].Borders = DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom | DevExpress.XtraPrinting.BorderSide.Right;
}
else //中间的表格
{
tb.Rows[rindex].Cells[cellcount].Borders = DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Bottom;
}
}
lst.RemoveAt(lst.Count - 1);
}
iTreeHeight--;
}