Winform(XtraReport)實現打印方法(轉載)(只是自我參考有錯誤的地方歡迎各位大佬前來交流心得)

Winform(XtraReport)實現打印方法(轉載)

首先新建一個XtraReport類。根據須要設計報表頁面佈局;

佈局設計完畢後,寫代碼綁定數據;html

[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屬性:this

  • 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
相關文章
相關標籤/搜索