#region 打印
//DataTable dtcols = new DataTable();
GridView gridview = new GridView();
int intRowHeight = 25;
private int headLevel = 1; //列头级别
private int[] headWidthArray; //列头总宽
///
/// 打印gridview
///
/// gridview
/// 表头级别
/// 表头各级宽度对应的数组
public void PrintingGridView(GridView gv,int headlevel,int[] headWidthArr)
{
//InitializeComponent();
//this.dtcols=dtcols;
this.gridview = gv;
headLevel = headlevel+1;![DevExpress根据横向扩展表格生成打印表格_第1张图片](http://img.e-com-net.com/image/info8/e6787ed124574a48884b9aa83dfae689.jpg)
headWidthArray = headWidthArr;
LoadReport();
}
private void LoadReport()
{
XtraReport xr = new XtraReport();
int pageHeight = 1169;
int pageWidth = 827;
xr.PaperKind = PaperKind.A4;//827,1169
xr.Font = new Font("宋体", 10.5f);
xr.TextAlignment = TextAlignment.MiddleCenter;
//内容
DetailBand Detail = new DetailBand();
Detail.Height = 1000;
Detail.Name = "Detail";
//横表自动生成Table
if (1 == 1)
{
XRTable tblInits = new XRTable();
tblInits.HeightF = 0;
tblInits.WidthF = 750;
tblInits.Borders = BorderSide.All;
xr.Margins.Right = 35;
xr.Margins.Left = 35;
xr.Margins.Top = 100;
xr.Margins.Bottom = 100;
string oldcell = "",
newcell = "";
string[] vercellArr = new string[headLevel]; //纵向表头合并的列数组,相当于表头列有对应的开关
int[] vercellSame = new int[headLevel];//同一列相同行数
int headTotalWidth = 0;
for (int v = 0; v < headLevel; v++)
{
vercellSame[v] = 1;//初始化数组
headTotalWidth += headWidthArray[v];
}
int cellno = -1;
for (int s = 0; s < this.gridview.RowCount; s++)
{
XRTableRow tbrNewArrs = new XRTableRow();
tbrNewArrs.HeightF = 35;
tblInits.HeightF += 35;
tbrNewArrs.WidthF = 750;
cellno = -1; //列索引开关
for (int k = 0; k < this.gridview.Columns.Count; k++)
{
XRTableCell trcNewArrs = new XRTableCell();
trcNewArrs.WidthF = (750) / (this.gridview.Columns.Count - headLevel);
trcNewArrs.Text = this.gridview.GetRowCellValue(s, this.gridview.Columns[k].FieldName).ToString();
newcell = trcNewArrs.Text;
if (this.gridview.GetRowCellValue(s, this.gridview.Columns[0].FieldName).ToString() == "日期")
{
trcNewArrs.Text = trcNewArrs.Text.Replace("00:00:00", "").TrimEnd();
trcNewArrs.Font = new Font("宋体", 9f);
}
#region 行列合并列头
if (k < headLevel)
{
trcNewArrs.WidthF = headWidthArray[k];
}
if (0 < k && k < headLevel) //横向合并列头,从第2列开始
{
if (oldcell == newcell)
{
oldcell = trcNewArrs.Text;
tbrNewArrs.Cells[cellno].WidthF += headWidthArray[k];
continue;
}
}
if (k < headLevel && s > 0) //纵向合并列头,从第2行开始
{
if (newcell == vercellArr[k]) //与上一行是否相等
{
trcNewArrs.Text = "";
trcNewArrs.Borders = BorderSide.Left | BorderSide.Right;
vercellSame[k]++;
}
else
{
if (vercellSame[k] != 1) //说明有相同的行
{
int rStartIndex = s - vercellSame[k]; //例如 -后面代表行索引,现在求C的合并:A-0,B-1,C-2,C-3,C-4,D-5,那么vercellSame[k]就代表了C的数量3,现在的行索引是D的索引5
int rTextIndex = rStartIndex + vercellSame[k] / 2;
if (vercellSame[k] % 2 == 0)
{
rTextIndex = rTextIndex - 1;//取上方单元格,bottom
}
tblInits.Rows[rStartIndex].Cells[k].Text = "";
if (vercellArr[k].Length > 2) //文字超过了2个,就要折半处理,没有循环处理
{
tblInits.Rows[rTextIndex].Cells[k].Text = vercellArr[k].Substring(0, vercellArr[k].Length / 2).Trim();
tblInits.Rows[rTextIndex + 1].Cells[k].Text = vercellArr[k].Substring(vercellArr[k].Length / 2, vercellArr[k].Length - vercellArr[k].Length / 2).Trim();
}
else
{
tblInits.Rows[rTextIndex].Cells[k].Text = vercellArr[k];
}
tblInits.Rows[rStartIndex].Cells[k].Borders = BorderSide.Left | BorderSide.Right;
tblInits.Rows[s - 1].Cells[k].Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
vercellSame[k] = 1;//重置开关
}
}
}
#endregion
oldcell = trcNewArrs.Text;
tbrNewArrs.Cells.Add(trcNewArrs);
cellno++;
}
tblInits.Rows.Add(tbrNewArrs); //行遍历
for (int head = 0; head < headLevel; head++)
{
vercellArr[head] = this.gridview.GetRowCellValue(s, this.gridview.Columns[head].FieldName).ToString();
}
}
Detail.Controls.Add(tblInits);
PageHeaderBand pageHeader = new PageHeaderBand();
XRLine lineHeader = new XRLine();
lineHeader.HeightF = 1;
lineHeader.WidthF = tblInits.WidthF;
lineHeader.LocationF = new PointF(0, 0);
pageHeader.HeightF = 1;
pageHeader.Controls.Add(lineHeader);
//换页时候最后一行第1列的纵向单元格border处理行索引为 (pageheight- xr.Margins.Top-xr.Margins.bottom)/rowheight-1=26=(1169-100-100)/35-1
tblInits.Rows[26].Cells[0].Borders = BorderSide.Left | BorderSide.Right | BorderSide.Bottom;
xr.Bands.Add(pageHeader);
}
xr.ShowPreviewMarginLines = false;
xr.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { Detail }); //, ReportHeader, ReportFooter
xr.ShowPreview();
}
#endregion