Winform(XtraReport)實現打印方法(轉載)
2017年09月16日 08:41:05 浮生丿未歇 閱讀數 4698更多
首先新建一個XtraReport類。根據須要設計報表頁面佈局;
![](http://static.javashuo.com/static/loading.gif)
佈局設計完畢後,寫代碼綁定數據;html
- 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();
- }
![](http://static.javashuo.com/static/loading.gif)
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