星空雅梦

Winform(XtraReport)实现打印方法(转载)

首先新建一个XtraReport类。根据需要设计报表页面布局;

布局设计完毕后,写代码绑定数据;

[csharp] view plain copy
 
 
 
 print?
  1. using System;  
  2. using System.Drawing;  
  3. using System.Collections;  
  4. using System.ComponentModel;  
  5. using DevExpress.XtraReports.UI;  
  6. using System.Data;  
  7. using Zeda.AssistantClass;  
  8.   
  9. namespace LYWJMIS  
  10. {  
  11.     public partial class MyReport2 : DevExpress.XtraReports.UI.XtraReport  
  12.     {  
  13.         private DataRow drPur;  
  14.   
  15.         public MyReport2()  
  16.         {  
  17.             InitializeComponent();  
  18.         }  
  19.         /// <summary>  
  20.         /// 带参数的构造函数  
  21.         /// </summary>  
  22.         /// <param name="drPur">采购单信息</param>  
  23.         public MyReport2(DataRow drPur)  
  24.             : this()  
  25.         {  
  26.             this.drPur = drPur;  
  27.             string sheetID = string.Empty;  
  28.             if (drPur == null) return;  
  29.             //绑定采购单信息  
  30.             BindFormData(drPur);  
  31.             //获取采购单ID  
  32.             sheetID = drPur["ID"].ToString();  
  33.             //获取采购单明细数据集  
  34.             DataSet dsDetail = DataService.Instance.GetPurchaseSheetDetailInfoBySheetID(sheetID);  
  35.             //绑定采购单明细信息  
  36.             BindTableData(dsDetail);  
  37.         }  
  38.         /// <summary>  
  39.         /// 绑定采购单明细信息  
  40.         /// </summary>  
  41.         private void BindTableData(DataSet ds)  
  42.         {  
  43.             //为XRTable的每一列绑定数据集及对应的字段  
  44.             this.xrTableCell1.DataBindings.Add("Text", ds, "DB0137A");//名称 DB0137A为字段名称  
  45.             this.xrTableCell2.DataBindings.Add("Text", ds, "DB0152A");//规格  
  46.             this.xrTableCell3.DataBindings.Add("Text", ds, "DB0150A");//单位  
  47.             this.xrTableCell7.DataBindings.Add("Text", ds, "DB0151A");//产地  
  48.             this.xrTableCell8.DataBindings.Add("Text", ds, "DB0168A");//剂型  
  49.             this.xrTableCell9.DataBindings.Add("Text", ds, "DB0183A");//计量规格  
  50.             this.xrTableCell10.DataBindings.Add("Text", ds, "DB0188A", "{0:n2}");//进价  
  51.             this.xrTableCell11.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量  
  52.             //设置本页小计(数量小计)  
  53.             this.xrTableCell23.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量  
  54.             this.xrTableCell23.Summary = new XRSummary(SummaryRunning.Page, SummaryFunc.Sum, string.Empty);  
  55.             //绑定数量合计  
  56.             this.xrTableCell18.DataBindings.Add("Text", ds, "DB0354A", "{0:f0}");//数量  
  57.             this.xrTableCell18.Summary = new XRSummary(SummaryRunning.Group, SummaryFunc.Sum, string.Empty);  
  58.         }  
  59.        /// <summary>  
  60.         /// 绑定采购单明细信息  
  61.        /// </summary>  
  62.         private void BindFormData(DataRow dr)  
  63.         {  
  64.             DataSet ds = DataSetOperator.DataRowToDataSet(dr);  
  65.             //XRLabel绑定数据 方法1:  
  66.             this.txtDB0336A.Text = dr["DB0336A"].ToString();  
  67.             //XRLabel绑定数据 方法2:  
  68.             this.txtDB0337A.DataBindings.Add(new XRBinding("Text", ds, "DB0337A", "{0:yyyy-MM-dd}"));  
  69.             this.txtDB0005A.Text = dr["DB0005A"].ToString();  
  70.             this.txtDB0339A.Text = dr["DB0339A"].ToString();  
  71.             this.txtDB0345A.DataBindings.Add(new XRBinding("Text", ds, "DB0345A", "{0:n2}"));  
  72.             this.labPrintTime.Text = DateTime.Now.Date.ToString();  
  73.   
  74.         }  
  75.     }  
  76. }  

调用MyReport2报表类打印预览

[csharp] view plain copy
 
 
 
 print?
  1.  private void btnPrintReport_Click(object sender, EventArgs e)  
  2.         {  
  3.             DataRow dr = this.wgcPur.GridView1.GetFocusedDataRow();  
  4.             if (dr == null) return;  
  5.             MyReport2 rep = new MyReport2(dr);  
  6.             //设置纸张类型为自定义  
  7.             rep.PaperKind = System.Drawing.Printing.PaperKind.Custom;  
  8.             //设置纸张大小  
  9.             double width = 24.1 * 0.3937008 * Dpi.GetDeviceCapsX();  
  10.             double height = 9.3 * 0.3937008 * Dpi.GetDeviceCapsY();  
  11.             rep.PageSize = new System.Drawing.Size((int)width, (int)height);  
  12.             //打印预览  
  13.             rep.ShowPreview();  
  14.         }  

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
原文地址:https://www.cnblogs.com/LiZhongZhongY/p/11589636.html