C#打印模板设计,E店宝打印模板设置,winfrom打印模板设计,DevExpress.XtraReports.UI.XRTable 表格代码生成。
一、打印效果
公共方法:
/// <summary> /// XRTable 表新增每行数据 /// </summary> /// <param name="xt">XRTable 表格对象</param> /// <param name="ds">数据表DataSet,这里只用到它下面的第二表(DataTable)ds.Tables[1]</param> /// <param name="ID">传入的筛选条件</param> public void LoadPrintData(DevExpress.XtraReports.UI.XRTable xt, System.Data.DataSet ds, string ID) { //获取报表对应的数据集DataSet; //赛选 单号 = ID 的每行数据 System.Data.DataRow[] dt = ds.Tables[1].Select("单号='" + ID + "'"); for (int i = xt.Rows.Count; i > 0; i--) { xt.Rows.Remove(xt.Rows[i]); } //遍历System.Data.DataRow[] dt 每一行,然后为XRTable xt添加每行数据 for (int i = 0; i < dt.Length; i++) { XRTableRow xrRow = new XRTableRow(); for (int t = 0; t < xt.Rows[0].Cells.Count; t++) { XRTableCell xrRowCell = new XRTableCell(); xrRowCell.CanGrow = true; xrRowCell.CanShrink = true; xrRowCell.Font = xt.Rows[0].Cells[0].Font;//设置字体 xrRowCell.TextAlignment = xt.Rows[0].Cells[0].TextAlignment;// xrRowCell.Text = dt[i][xt.Rows[0].Cells[t].Text].ToString(); xrRowCell.Width = xt.Rows[0].Cells[t].Width; xrRowCell.Height = xt.Rows[0].Cells[t].Height; xrRowCell.BorderColor = xt.Rows[0].Cells[t].BorderColor; xrRowCell.Borders = DevExpress.XtraPrinting.BorderSide.All; xrRowCell.BorderWidth = xt.Rows[0].Cells[t].BorderWidth; //单元格的Padding值的设置 DevExpress.XtraPrinting.PaddingInfo padRight = new DevExpress.XtraPrinting.PaddingInfo(); padRight.Top = xt.Rows[0].Cells[0].Padding.Top; padRight.Bottom = xt.Rows[0].Cells[0].Padding.Bottom; padRight.Left = xt.Rows[0].Cells[0].Padding.Left; padRight.Right = xt.Rows[0].Cells[0].Padding.Right; xrRowCell.Padding = padRight; //XRTableRow行 添加单元格 xrRow.Cells.Add(xrRowCell); } //XRTable 添加行 xt.Rows.Add(xrRow); } }
二、代码编辑
1 、table1 : table控件的Name;
2、label33 :label控件 实现绑定【外部平台单号】的控件;
3、label32:绑定【E店宝订单编号】(S开头的单号);
不同模板请以对应的控件Name属性值类 来替换以上3个值(table1,label33,label32)
string tid = label32.Text;//绑定E店宝【订单编号】(S开头的单号); string OutTid = label33.Text;//绑定E店宝【外部平台单号】 //获取报表对应的数据集DataSet; System.Data.DataSet ds=detailBand1.Report.DataSource as System.Data.DataSet; //如果获取到了DataSet,并且Table数据是1个以上,则取索引是第1个的Table,此Table定义为是明细数据。 if (ds!=null && ds.Tables.Count>1) { System.Data.DataTable dt=ds.Tables[1]; //循环读取Table中的每一个数据行形成明细的拼接字符串。 System.Data.DataView dv = dt.DefaultView; dv.Sort = "报价总金额 DESC , 条形码 ASC "; //排序 System.Data.DataTable dt2 = dv.ToTable(); table1.Rows.Clear(); /***************************外部平台单号*********************************/ XRTableRow xrRowOutTid = new XRTableRow(); XRTableCell xrRowOutTidTile = new XRTableCell(); XRTableCell xrRowOutTidValue = new XRTableCell(); xrRowOutTidTile.Width = 160 ; xrRowOutTidValue.Width = 360 ; //-------------------------------------------- xrRowOutTidTile.CanGrow = true; xrRowOutTidTile.CanShrink = false; xrRowOutTidTile.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowOutTidTile.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //-------------------------------------------- xrRowOutTidValue.CanGrow = true; xrRowOutTidValue.CanShrink = false; xrRowOutTidValue.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowOutTidValue.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; //-------------------------------------------- xrRowOutTidTile.Text = "订单号"; xrRowOutTidValue.Text =OutTid;//外部平台单号 //-------------------------------------------- xrRowOutTid.Cells.Add(xrRowOutTidTile); xrRowOutTid.Cells.Add(xrRowOutTidValue); table1.Rows.Add(xrRowOutTid); /***************************发货单号*********************************/ XRTableRow xrRowTid = new XRTableRow(); XRTableCell xrRowTidTitle = new XRTableCell(); XRTableCell xrRowTidValue = new XRTableCell(); xrRowTidTitle.Width = 160 ; xrRowTidValue.Width = 360 ; //-------------------------------------------- xrRowTidTitle.CanGrow = true; xrRowTidTitle.CanShrink = false; xrRowTidTitle.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTidTitle.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //-------------------------------------------- xrRowTidValue.CanGrow = true; xrRowTidValue.CanShrink = false; xrRowTidValue.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTidValue.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; //-------------------------------------------- xrRowTidTitle.Text = "发货单号"; xrRowTidValue.Text =tid; //-------------------------------------------- xrRowTid.Cells.Add(xrRowTidTitle); xrRowTid.Cells.Add(xrRowTidValue); table1.Rows.Add(xrRowTid); /***************************列标题*********************************/ XRTableRow xrRowTitle = new XRTableRow(); XRTableCell xrRowTitleName = new XRTableCell();//产品名称 XRTableCell xrRowTitleHH = new XRTableCell();//货号 XRTableCell xrRowTitleBarCode = new XRTableCell();//"条码" XRTableCell xrRowTitleNum = new XRTableCell();//"数量" //----------------------------- xrRowTitleBarCode .CanGrow = true; xrRowTitleBarCode .CanShrink = false; xrRowTitleBarCode .Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTitleBarCode .TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //----------------------------- xrRowTitleHH.CanGrow = true; xrRowTitleHH.CanShrink = false; xrRowTitleHH.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTitleHH.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //----------------------------- xrRowTitleName.CanGrow = true; xrRowTitleName.CanShrink = false; xrRowTitleName.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTitleName.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; xrRowTitleHH.Width = 160 ; xrRowTitleName.Width = 300 ; xrRowTitleNum.Width = 60 ; xrRowTitleBarCode.Width = 160 ; //----------------------------- xrRowTitleNum.CanGrow = true; xrRowTitleNum.CanShrink = false; xrRowTitleNum.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrRowTitleNum.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; //----------------------------- xrRowTitleName.Text = "产品名称"; xrRowTitleHH.Text = "货号"; xrRowTitleBarCode.Text = "条码"; xrRowTitleNum.Text = "数量" ; //----------------------------- xrRowTitle.Cells.Add(xrRowTitleName);//"产品名称" xrRowTitle.Cells.Add(xrRowTitleHH);//"货号" xrRowTitle.Cells.Add(xrRowTitleBarCode); //产品条形码 xrRowTitle.Cells.Add(xrRowTitleNum);//数量 table1.Rows.Add(xrRowTitle); /***************************循环添加明细*********************************/ for(int rowIndex=0;rowIndex<dt2.Rows.Count;rowIndex++) { if (dt2.Rows[rowIndex]["订单编号"].ToString()==tid ) { XRTableRow xrForRow = new XRTableRow(); XRTableCell xrForRowSPC = new XRTableCell();//软件规格 XRTableCell xrForRowRem = new XRTableCell();//产品备注 XRTableCell xrForRowNum = new XRTableCell();//订货数量 XRTableCell xrForRowBarCode = new XRTableCell();//条形码 //-------------------------------------- xrForRowBarCode .CanGrow = true; xrForRowBarCode.CanShrink = false; xrForRowBarCode.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrForRowBarCode.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //------------------------------------- xrForRowSPC.CanGrow = true; xrForRowSPC.CanShrink = false; xrForRowSPC.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrForRowSPC.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; //------------------------------------- xrForRowRem.CanGrow = true; xrForRowRem.CanShrink = false; xrForRowRem.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrForRowRem.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; xrForRowSPC.Width = 160 ; xrForRowRem.Width = 300 ; xrForRowNum.Width = 60 ; xrForRowBarCode.Width = 160 ; //------------------------------------- xrForRowNum.CanGrow = true; xrForRowNum.CanShrink = false; xrForRowNum.Font = new System.Drawing.Font("微软雅黑", 8F, System.Drawing.FontStyle.Bold); xrForRowNum.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; //------------------------------------- xrForRowSPC.Text = dt2.Rows[rowIndex]["软件规格"].ToString(); xrForRowRem.Text = dt2.Rows[rowIndex]["产品备注"].ToString() ; xrForRowNum.Text = dt2.Rows[rowIndex]["订货数量"].ToString() ; xrForRowBarCode.Text = dt2.Rows[rowIndex]["条形码"].ToString() ; //------------------------------------- xrForRowSPC.WordWrap = true; xrForRow.Cells.Add(xrForRowRem);// xrForRow.Cells.Add(xrForRowSPC); //软件规格 xrForRow.Cells.Add(xrForRowBarCode); // xrForRow.Cells.Add(xrForRowNum);// //------------------------------------- table1.Rows.Add(xrForRow); } } }
三、添加行列的公共方法
/// <summary> /// 替换特殊字符 /// </summary> /// <param name="str"></param> /// <returns></returns> public string ReStr(string str) { return str.Replace(";", ".").Replace(",", ".").Replace(";", ".").Replace(",", "."); } /// <summary> /// 添加行 /// </summary> /// <param name="table1">DevExpress.XtraReports.UI.XRTable 对象</param> /// <param name="RowsStr">格式:"文本,宽度,字体大小,字体,是否加粗,对其方式:左、右、中;";</param> public void TableAddRow(DevExpress.XtraReports.UI.XRTable table1, string RowsStr) { RowsStr = RowsStr.Replace(";", ";").Replace(",", ","); string[] Zstr = RowsStr.Split(';'); string 文本 = "", 宽度 = "100", 字体大小 = "8F", 字体 = "微软雅黑", 是否加粗 = "否",对其方式="左"; XRTableRow xrRow = new XRTableRow(); for (int i = 0; i < Zstr.Length; i++) { 文本 = Zstr[i].Split(',')[0]; 宽度 = Zstr[i].Split(',')[1].Replace(" ", ""); 字体大小 = Zstr[i].Split(',')[2].Replace(" ", ""); 字体 = Zstr[i].Split(',')[3].Replace(" ", "");//"微软雅黑" 是否加粗 = Zstr[i].Split(',')[4].Replace(" ",""); 对其方式 = Zstr[i].Split(',')[5].Replace(" ",""); //--------------------------------- float f字体大小 = float.Parse(字体大小); //--------------------------------- XRTableCell xrRowCell = new XRTableCell(); xrRowCell.CanGrow = true; xrRowCell.CanShrink = false; xrRowCell.Width = Convert.ToInt32(宽度); //--------------------------------- //---是否加粗 System.Drawing.FontStyle fstyle = System.Drawing.FontStyle.Bold; if (是否加粗 == "否") { fstyle = System.Drawing.FontStyle.Regular; } //--------------------------------- xrRowCell.Font = new System.Drawing.Font(字体, f字体大小, fstyle); //--------------------------------- //对其方式 DevExpress.XtraPrinting.TextAlignment txtalign = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; if (对其方式=="左") { txtalign = DevExpress.XtraPrinting.TextAlignment.MiddleLeft; } if (对其方式 == "右") { txtalign = DevExpress.XtraPrinting.TextAlignment.MiddleRight; } if (对其方式 == "中") { txtalign = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; } xrRowCell.TextAlignment = txtalign; //----------------------------------- xrRowCell.Text = 文本; xrRow.Cells.Add(xrRowCell);//"产品名称" } table1.Rows.Add(xrRow); }
调用:
/***************************外部平台单号*********************************/ TableAddRow(table1, "订单号,160,16,微软雅黑,是,左;"+OutTid+",360,16,微软雅黑,是,左"); /***************************发货单号*********************************/ TableAddRow(table1, "发货单号,160,16,微软雅黑,是,左;"+tid+",360,16,微软雅黑,是,左"); /***************************列标题*********************************/ TableAddRow(table1, "产品名称,300,8,微软雅黑,是,左;"//第一列 +"货号,160,8,微软雅黑,是,左;"//第二列 +"条码,160,8,微软雅黑,是,左;"//第三列 +"数量,60,8,微软雅黑,是,中"//第四列 );
通用方法调用:
private void label13_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { string tid = GetCurrentColumnValue("订单编号").ToString();//绑定E店宝【订单编号】(S开头的单号); string OutTid = GetCurrentColumnValue("外部平台单号").ToString();// label33.Text;//绑定E店宝【外部平台单号】 //获取报表对应的数据集DataSet; System.Data.DataSet ds=detailBand1.Report.DataSource as System.Data.DataSet; //如果获取到了DataSet,并且Table数据是1个以上,则取索引是第1个的Table,此Table定义为是明细数据。 if (ds!=null && ds.Tables.Count>1) { System.Data.DataTable dt=ds.Tables[1]; //循环读取Table中的每一个数据行形成明细的拼接字符串。 System.Data.DataView dv = dt.DefaultView; dv.Sort = "报价总金额 DESC , 条形码 ASC "; //排序 System.Data.DataTable dt2 = dv.ToTable(); table1.Rows.Clear(); //"文本,宽度,字体大小,字体,是否加粗,对其方式:左、右、中"; //TableAddRowBarCode(table1, "快递单号,160,16,微软雅黑,是,左",barCode1); /***************************外部平台单号*********************************/ TableAddRow(table1, "订单号,160,16,微软雅黑,是,左;"+OutTid+",360,16,微软雅黑,是,左"); /***************************发货单号*********************************/ TableAddRow(table1, "发货单号,160,16,微软雅黑,是,左;"+tid+",360,16,微软雅黑,是,左"); /***************************列标题*********************************/ TableAddRow(table1, "产品名称,300,8,微软雅黑,是,左;"//第一列 +"货号,160,8,微软雅黑,是,左;"//第二列 +"条码,160,8,微软雅黑,是,左;"//第三列 +"数量,60,8,微软雅黑,是,中"//第四列 ); /***************************循环添加明细*********************************/ for(int rowIndex=0;rowIndex<dt2.Rows.Count;rowIndex++) { if (dt2.Rows[rowIndex]["订单编号"].ToString()==tid ) { string 软件规格 = dt2.Rows[rowIndex]["软件规格"].ToString();软件规格 = ReStr(软件规格); string 产品备注 = dt2.Rows[rowIndex]["产品备注"].ToString() ;产品备注 = ReStr(产品备注); string 订货数量 = dt2.Rows[rowIndex]["订货数量"].ToString() ; string 条形码 = dt2.Rows[rowIndex]["条形码"].ToString() ; TableAddRow(table1, 产品备注 + ",300,8,微软雅黑,是,左;"//第一列产品名称 +软件规格+",160,8,微软雅黑,是,左;"//第二列货号 +条形码+",160,8,微软雅黑,是,左;"//第三列条码 +订货数量+",60,8,微软雅黑,是,中"//第四列数量 ); } } } }
方法2:
在工具栏 拖拽 一个 table 到模板上面,然后添加列,每列的内容填写数据源字段名称,不要填写数据源字段,否则打印出错!
public void LoadPrintData(DevExpress.XtraReports.UI.XRTable xt, System.Data.DataSet ds) { //获取报表对应的数据集DataSet; System.Data.DataTable dt = ds.Tables[1]; for (int i = 0; i < dt.Rows.Count; i++) { XRTableRow xrRow = new XRTableRow(); for (int t = 0; t < xt.Rows[0].Cells.Count; t++) { XRTableCell xrRowCell = new XRTableCell(); xrRowCell.Font = xt.Rows[0].Cells[0].Font;//字体样式采用行首的字体样式 xrRowCell.TextAlignment = xt.Rows[0].Cells[0].TextAlignment;//对齐方式同上 xrRowCell.Text = dt.Rows[i][xt.Rows[0].Cells[t].Text].ToString();//获取数据源字段数据 xrRowCell.Width = xt.Rows[0].Cells[t].Width; xrRow.Cells.Add(xrRowCell);//"产品名称" } xt.Rows.Add(xrRow); } }
public void LoadPrintData(DevExpress.XtraReports.UI.XRTable xt, System.Data.DataSet ds) { //获取报表对应的数据集DataSet; System.Data.DataTable dt = ds.Tables[1]; for (int i = 0; i < dt.Rows.Count; i++) { XRTableRow xrRow = new XRTableRow(); for (int t = 0; t < xt.Rows[0].Cells.Count; t++) { XRTableCell xrRowCell = new XRTableCell(); xrRowCell.Font = xt.Rows[0].Cells[0].Font; xrRowCell.TextAlignment = xt.Rows[0].Cells[0].TextAlignment; xrRowCell.Text = dt.Rows[i][xt.Rows[0].Cells[t].Text].ToString(); xrRowCell.Width = xt.Rows[0].Cells[t].Width; xrRowCell.BorderColor = xt.Rows[0].Cells[t].BorderColor; xrRowCell.Borders = DevExpress.XtraPrinting.BorderSide.All; xrRowCell.BorderWidth = xt.Rows[0].Cells[t].BorderWidth; xrRow.Cells.Add(xrRowCell);//"产品名称" } xt.Rows.Add(xrRow); } }
private void detailBand1_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e) { //获取报表对应的数据集DataSet; System.Data.DataSet ds=detailBand1.Report.DataSource as System.Data.DataSet; string ID = GetCurrentColumnValue("ID").ToString(); LoadPrintData(table2, ds,ID); } public void LoadPrintData(DevExpress.XtraReports.UI.XRTable xt, System.Data.DataSet ds,string ID) { //获取报表对应的数据集DataSet; System.Data.DataRow[] dt = ds.Tables[1].Select("ID='" + ID+ "'"); for (int i = 0; i < dt.Length;i++ ) { XRTableRow xrRow = new XRTableRow(); for (int t = 0; t < xt.Rows[0].Cells.Count; t++) { XRTableCell xrRowCell = new XRTableCell(); xrRowCell.CanGrow = true; xrRowCell.CanShrink = true; xrRowCell.Font = xt.Rows[0].Cells[0].Font; xrRowCell.TextAlignment = xt.Rows[0].Cells[0].TextAlignment; xrRowCell.Text = dt[i][xt.Rows[0].Cells[t].Text].ToString(); xrRowCell.Width = xt.Rows[0].Cells[t].Width; xrRowCell.BorderColor = xt.Rows[0].Cells[t].BorderColor; xrRowCell.Borders = DevExpress.XtraPrinting.BorderSide.All; xrRowCell.BorderWidth = xt.Rows[0].Cells[t].BorderWidth; xrRow.Cells.Add(xrRowCell);//"产品名称" } xt.Rows.Add(xrRow); } }
显示效果图: