////////////////////////////效果图//////////////////////////////////
/////////////////////////////代码部分//////////////////////////////////
using System; using System.Drawing; using System.Collections; using System.ComponentModel; using DevExpress.XtraReports.UI; using System.Collections.Generic; using MvcApplication1.Models; using System.Data; /// <summary> /// Summary description for XtraReport1 /// </summary> public class XtraReport1 : DevExpress.XtraReports.UI.XtraReport { private DevExpress.XtraReports.UI.DetailBand Detail; private DevExpress.XtraReports.UI.TopMarginBand TopMargin; private DevExpress.XtraReports.UI.BottomMarginBand BottomMargin; protected int XRTableCellHeight = 38; private ReportHeaderBand ReportHeader; private XRLabel xrLabel2; /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; public XtraReport1() { InitializeComponent(); XtraReport rpt = this;// 建立报表实例 rpt.DataSource = FillDataset();//设置报表数据源 InitBands(rpt);//添加带区(Bands) InitDetailsBasedonXRTable(rpt);//用XRTable显示报表 } public DataSet FillDataset() { DataSet myDataSet = new DataSet(); myDataSet.DataSetName = "myDataSet"; DataTable table = new DataTable("Detail"); myDataSet.Tables.Add(table); table.Columns.Add("Name", typeof(String)); table.Columns.Add("Address", typeof(String)); table.Columns.Add("Sex", typeof(String)); table.Columns.Add("Birthplace", typeof(String)); table.Columns.Add("Birthday", typeof(String)); for (int i = 0; i < 50; i++) { table.Rows.Add(new object[] { i, "辽宁", "女", "辽宁", "1983-XX-XX" }); table.Rows.Add(new object[] { i, "广东", "男", "辽宁", "1984-10-XX" }); table.Rows.Add(new object[] { i, "北京", "男", "北京", "1985-XX-XX" }); table.Rows.Add(new object[] { i, "上海", "女", "湖南", "1984-XX-XX" }); table.Rows.Add(new object[] { i, "广东", "女", "辽宁", "1985-2-XX" }); } return myDataSet; } public void InitBands(XtraReport rpt) { DetailBand detail = new DetailBand(); PageHeaderBand pageHeader = new PageHeaderBand(); ReportFooterBand reportFooter = new ReportFooterBand(); detail.Height = XRTableCellHeight; reportFooter.Height = 380; pageHeader.Height = XRTableCellHeight; rpt.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { detail, pageHeader, reportFooter }); } public void InitDetailsBasedonXRTable(XtraReport rpt) { DataSet ds = ((DataSet)rpt.DataSource); int colCount = ds.Tables[0].Columns.Count; int colWidth = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right)) / colCount; // Create a table to represent headers XRTable tableHeader = new XRTable(); tableHeader.Height = XRTableCellHeight; tableHeader.Width = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right)); tableHeader.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; tableHeader.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Bold); // Create a table to display data XRTable tableDetail = new XRTable(); tableDetail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; tableDetail.Width = (rpt.PageWidth - (rpt.Margins.Left + rpt.Margins.Right)); tableDetail.Height = XRTableCellHeight; tableDetail.Font = new System.Drawing.Font("宋体", 12.75F, System.Drawing.FontStyle.Regular); XRTableRow headerRow = new XRTableRow(); XRTableRow detailRow = new XRTableRow(); // Create table cells, fill the header cells with text, bind the cells to data for (int i = 0; i < colCount; i++) { XRTableCell headerCell = new XRTableCell(); headerCell.Width = colWidth; headerCell.Borders = DevExpress.XtraPrinting.BorderSide.All; headerCell.Text = ds.Tables[0].Columns[i].Caption; XRTableCell detailCell = new XRTableCell(); detailCell.Width = colWidth; detailCell.DataBindings.Add("Text", null, ds.Tables[0].Columns[i].Caption); detailCell.Borders = DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom; // Place the cells into the corresponding tables headerRow.Cells.Add(headerCell); detailRow.Cells.Add(detailCell); } headerRow.Width = tableHeader.Width; tableHeader.Rows.Add(headerRow); detailRow.Width = tableDetail.Width; tableDetail.Rows.Add(detailRow); // Place the table onto a report's Detail band rpt.Bands[BandKind.PageHeader].Controls.Add(tableHeader); rpt.Bands[BandKind.Detail].Controls.Add(tableDetail); } /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.Detail = new DevExpress.XtraReports.UI.DetailBand(); this.TopMargin = new DevExpress.XtraReports.UI.TopMarginBand(); this.BottomMargin = new DevExpress.XtraReports.UI.BottomMarginBand(); this.ReportHeader = new DevExpress.XtraReports.UI.ReportHeaderBand(); this.xrLabel2 = new DevExpress.XtraReports.UI.XRLabel(); ((System.ComponentModel.ISupportInitialize)(this)).BeginInit(); // // Detail // this.Detail.HeightF = 0F; this.Detail.Name = "Detail"; this.Detail.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F); this.Detail.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft; // // TopMargin // this.TopMargin.HeightF = 36.45833F; this.TopMargin.Name = "TopMargin"; this.TopMargin.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F); this.TopMargin.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft; // // BottomMargin // this.BottomMargin.HeightF = 36.45833F; this.BottomMargin.Name = "BottomMargin"; this.BottomMargin.Padding = new DevExpress.XtraPrinting.PaddingInfo(0, 0, 0, 0, 100F); this.BottomMargin.TextAlignment = DevExpress.XtraPrinting.TextAlignment.TopLeft; // // ReportHeader // this.ReportHeader.Controls.AddRange(new DevExpress.XtraReports.UI.XRControl[] { this.xrLabel2}); this.ReportHeader.HeightF = 54.25002F; this.ReportHeader.Name = "ReportHeader"; // // xrLabel2 // this.xrLabel2.Font = new System.Drawing.Font("宋体", 15.75F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.xrLabel2.LocationFloat = new DevExpress.Utils.PointFloat(0F, 0F); this.xrLabel2.Name = "xrLabel2"; this.xrLabel2.Padding = new DevExpress.XtraPrinting.PaddingInfo(2, 2, 0, 0, 100F); this.xrLabel2.SizeF = new System.Drawing.SizeF(650F, 54.25002F); this.xrLabel2.StylePriority.UseFont = false; this.xrLabel2.StylePriority.UseTextAlignment = false; this.xrLabel2.Text = "等了好久,终于等到今天"; this.xrLabel2.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter; // // XtraReport3 // this.Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { this.Detail, this.TopMargin, this.BottomMargin, this.ReportHeader}); this.Margins = new System.Drawing.Printing.Margins(100, 100, 36, 36); this.Version = "15.2"; ((System.ComponentModel.ISupportInitialize)(this)).EndInit(); } #endregion }