Winform(XtraReport)实现打印方法(转载)
首先新建一个XtraReport类。根据需要设计报表页面布局;
布局设计完毕后,写代码绑定数据;
- using System;
- using System.Drawing;
- using System.Collections;
- using System.ComponentModel;
- using DevExpress.XtraReports.UI;
- using System.Data;
- using Zeda.AssistantClass;
- namespace LYWJMIS
- {
- public partial class MyReport2 : DevExpress.XtraReports.UI.XtraReport
- {
- private DataRow drPur;
- public MyReport2()
- {
- InitializeComponent();
- }
- /// <summary>
- /// 带参数的构造函数
- /// </summary>
- /// <param name="drPur">采购单信息</param>
- public MyReport2(DataRow drPur)
- : this()
- {
- this.drPur = drPur;
- string sheetID = string.Empty;
- if (drPur == null) return;
- //绑定采购单信息
- BindFormData(drPur);
- //获取采购单ID
- sheetID = drPur["ID"].ToString();
- //获取采购单明细数据集
- DataSet dsDetail = DataService.Instance.GetPurchaseSheetDetailInfoBySheetID(sheetID);
- //绑定采购单明细信息
- BindTableData(dsDetail);
- }
- /// <summary>
- /// 绑定采购单明细信息
- /// </summary>
- private void BindTableData(DataSet ds)
- {
- //为XRTable的每一列绑定数据集及对应的字段
- this.xrTableCell1.DataBindings.Add("Text", ds, "DB0137A");//名称 DB0137A为字段名称
- this.xrTableCell2.DataBindings.Add("Text", ds, "DB0152A");//规格
- this.xrTableCell3.DataBindings.Add("Text", ds, "DB0150A");//单位
- this.xrTableCell7.DataBindings.Add("Text", ds, "DB0151A");//产地
- this.xrTableCell8.DataBindings.Add("Text", ds, "DB0168A");//剂型
- this.xrTableCell9.DataBindings.Add("Text", ds, "DB0183A");//计量规格
- this.xrTableCell10.DataBindings.Add("Text", ds, "DB0188A", "{0:n2}");//进价
- this.xrTableCell11.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量
- //设置本页小计(数量小计)
- this.xrTableCell23.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量
- this.xrTableCell23.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Sum, string.Empty);
- //绑定数量合计
- this.xrTableCell18.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量
- this.xrTableCell18.Summary = new XRSummary(SummaryRunning.Group, SummaryFunc.Sum, string.Empty);
- }
- /// <summary>
- /// 绑定采购单明细信息
- /// </summary>
- private void BindFormData(DataRow dr)
- {
- DataSet ds = DataSetOperator.DataRowToDataSet(dr);
- //XRLabel绑定数据 方法1:
- this.txtDB0336A.Text = dr["DB0336A"].ToString();
- //XRLabel绑定数据 方法2:
- this.txtDB0337A.DataBindings.Add(new XRBinding("Text", ds, "DB0337A", "{0:yyyy-MM-dd}"));
- this.txtDB0005A.Text = dr["DB0005A"].ToString();
- this.txtDB0339A.Text = dr["DB0339A"].ToString();
- this.txtDB0345A.DataBindings.Add(new XRBinding("Text", ds, "DB0345A", "{0:n2}"));
- this.labPrintTime.Text = DateTime.Now.Date.ToString();
- }
- }
- }
调用MyReport2报表类打印预览
- private void btnPrintReport_Click(object sender, EventArgs e)
- {
- DataRow dr = this.wgcPur.GridView1.GetFocusedDataRow();
- if (dr == null) return;
- MyReport2 rep = new MyReport2(dr);
- //设置纸张类型为自定义
- rep.PaperKind = System.Drawing.Printing.PaperKind.Custom;
- //设置纸张大小
- double width = 24.1 * 0.3937008 * Dpi.GetDeviceCapsX();
- double height = 9.3 * 0.3937008 * Dpi.GetDeviceCapsY();
- rep.PageSize = new System.Drawing.Size((int)width, (int)height);
- //打印预览
- rep.ShowPreview();
- }
xrTableCell属性:
- TextAlignment:设置单元格显示内容的对齐方式;
- Text:设置单元格显示的字符;
- Styles-Style:设置单元的样式;
- CanGrow:设置单元中的内容是否能够换行。如果设置为true,则内容超出单元格长度时,会自动换行,同时,单元格高度自动增加;
- CanShrik:设置单元格的高度是否会随内容伸缩;
- Borders:设置单元格显示上、下、左、右的边框。
XtraReport属性:
- PaperKind:设置打印报表纸张的类型;
- PageHeight:设置报表的纸张的高度(单位:像素)。注意:只有当PaperKind=Custom时,才能设置此属性,否则无效;
- PageWidth:设置纸张的宽度(单位:像素)。注意:只有当PaperKind=Custom时,才能设置此属性,否则无效;
- PageColor:设置报表的背景颜色;
- Margins:设置纸张的页边距;
- DefaultPrinterSettingsUsing:打印报表是否应用默认打印机的默认设置(纸张类型、页边距等);
- UseMargins:如果值为true,则属性Margins将失去作用;
- UsePaperKind:如果值为true,则属性PerperKind,PageHeight,PageWidth将失去作用;
- GroupFooter-RepeatEveryPage:如果值为true,则页脚将在每一页显示;如果为false,则页脚只在第一页显示
转载地址:http://blog.csdn.net/ljunqiang/article/details/39498171#comments