//
 // ================================================= 
 //   基于DevExpress通用表格打印
 //    
 //    设计: 陈炎和 2011.02
 //==================================================
 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Drawing;
 using System.Collections;
 using System.ComponentModel;
 using DevExpress.XtraReports.UI;
 using System.Data;
 using System.Windows.Forms; namespace cyh.General
 {    
     /// <summary>
     /// 通用报表
     /// 每页的小页数暂不支持(需用子报表)
     /// </summary>
     public class ctlTableXReport : DevExpress.XtraReports.UI.XtraReport,iTableReport
     {       
                 
         /// <summary>
         /// 数据源
         /// </summary>
         public new DataTable DataSource
         {
             get { return (DataTable)base.DataSource; }
             set { base.DataSource = value; } 
         }        private ReportMain m_rpm = new ReportMain();
         private ArrayList m_rpt = new ArrayList();
         private ArrayList m_rpf = new ArrayList();
         private ReportBarcode m_rpb = new ReportBarcode();
         private int m_rowindex;
         /// <summary>
         /// 定义报表主体同时创建报表(必须先定义SetReportTitle和SetReportFiled)
         /// </summary>
         /// <param name="DetailHeight">明细每行高度</param>
         /// <param name="DetailRows">每页行数(0:自动计算)</param>
         /// <param name="GridLine">表格线的类型 1:三栏表,2:有边框的三栏,4:完全网格</param>
         /// <param name="Align">表格对齐方向</param>
         /// <param name="Landscape">横向还是纵向打印</param>
         /// <param name="Margins">页的边距尺寸</param>
         /// <param name="PaperKind">标准的纸张大小</param>
         /// <param name="pageSize">纸张大小</param>
         /// <param name="FromPage">起始页</param>
         /// <param name="ToPage">结束页</param>
         /// <param name="printerName">打印机名</param> 
         /// <param name="AutoPageHeight">根据行高度及行数计算纸长</param>
         /// <param name="Pages">每页的小页数</param>
         public void SetReportMain(int DetailHeight, int DetailRows, ReportGridLine GridLine, StringAlignment Align, bool Landscape, System.Drawing.Printing.Margins Margins,
             System.Drawing.Printing.PaperKind PaperKind, Size pageSize, int FromPage, int ToPage, string printerName,bool AutoPageHeight,int Pages)
         {
             if (AutoPageHeight && DetailRows == 0)
                 throw new Exception("自动页长时每页行数必须大于0");
             m_rpm.DetailHeight = DetailHeight;
             m_rpm.DetailRows = DetailRows;
             m_rpm.Align = Align;
             m_rpm.Landscape = Landscape;
             m_rpm.Margins = Margins;
             m_rpm.PaperKind = PaperKind;
             m_rpm.pageSize = pageSize;
             m_rpm.BeginPage = FromPage;
             m_rpm.EndPage = ToPage;
             m_rpm.printerName = printerName;
             m_rpm.AutoPageHeight = AutoPageHeight;
             m_rpm.GridLine = GridLine;
             base.Bands.Clear();
             this.SetReport(m_rpm, (ReportTitle[])m_rpt.ToArray(typeof(ReportTitle)), (ReportField[])m_rpf.ToArray(typeof(ReportField)),m_rpb);
         }
         public void SetReportMain(int DetailHeight, int DetailRows, ReportGridLine GridLine, Size pageSize, int FromPage, string printerName, int Pages)
         {
             SetReportMain(DetailHeight, DetailRows, GridLine, StringAlignment.Center, false, new System.Drawing.Printing.Margins(80, 80, 100, 80), System.Drawing.Printing.PaperKind.Custom, pageSize, FromPage, 0, printerName, false, 1);
         }
         public void SetReportMain(int DetailHeight, ReportGridLine GridLine, Size pageSize, int FromPage, string printerName)
         {
             SetReportMain(DetailHeight, 0, GridLine, pageSize, FromPage, printerName, 1);
         }        /// <summary>
         /// 定义报表标题
         /// </summary>
         /// <param name="Caption">标题内容</param>
         /// <param name="Header">页头或页脚</param>
         /// <param name="Font">字体(楷体_GB2312加粗四号)</param>
         /// <param name="TextAlignment">对齐方式</param>
         /// <param name="size"> 标题的高度和宽度,若不定义便检测Caption,否则自换行</param>
         public void SetReportTitle(string Caption, bool Header, Font Font, DevExpress.XtraPrinting.TextAlignment TextAlignment, System.Drawing.Size size)
         {
             ReportTitle rpt = new ReportTitle();
             rpt.Caption = Caption;
             rpt.Header = Header;
             rpt.xTextAlignment = TextAlignment == 0 ? DevExpress.XtraPrinting.TextAlignment.TopCenter : TextAlignment;
             rpt.Font = Font == null ? new System.Drawing.Font("楷体_GB2312", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134))) : Font;
             rpt.size = size;
             m_rpt.Add(rpt);
         }
         public void AddReportTitle(string Caption, bool Header, Font Font, StringAlignment TextAlignment, System.Drawing.Size size)
         {
             DevExpress.XtraPrinting.TextAlignment alig = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
             if (TextAlignment == StringAlignment.Center) alig = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
             if (TextAlignment == StringAlignment.Far) alig = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
             SetReportTitle(Caption, Header, Font, alig, size);
         }
         /// <summary>
         /// 定义报表标题
         /// </summary>
         /// <param name="Caption">标题内容</param>
         /// <param name="Header">页头或页脚</param>
         public void AddReportTitle(string Caption, bool Header)
         {
             AddReportTitle(Caption, Header, null, StringAlignment.Near, Size.Empty);
         }
         /// <summary>
         /// 定义报表字段
         /// </summary>
         /// <param name="Caption">表格头标题</param>
         /// <param name="HeaderFont">标题字体(楷体_GB2312、小四)</param>
         /// <param name="Field">绑定字段</param>
         /// <param name="TextAlignment">对齐方式</param>
         /// <param name="DetailFont"> 表格字体(楷体_GB2312、小四)</param>
         /// <param name="WordWrap">自动换行</param>
         /// <param name="Bottom">表格脚内容</param>
         /// <param name="Sum">是否合计</param>
         /// <param name="FormatString">格式化字符串</param>
         /// <param name="HeaderSize">标题的高度和宽度,若不定义便检测Caption,否则自换行并居中</param>
         public void SetReportField(string Caption, Font HeaderFont, string Field, DevExpress.XtraPrinting.TextAlignment TextAlignment, Font DetailFont
                         , bool WordWrap, string Bottom, bool Sum, string FormatString, Size HeaderSize)
         {
             ReportField rpf = new ReportField();
             rpf.Caption = Caption;
             rpf.HeaderFont = HeaderFont;
             rpf.Field = Field;
             rpf.xTextAlignment = TextAlignment;
             rpf.DetailFont = DetailFont;
             rpf.WordWrap = WordWrap;
             rpf.Bottom = Bottom;
             rpf.Sum = Sum;
             rpf.FormatString = FormatString;
             rpf.HeaderSize = HeaderSize;
             m_rpf.Add(rpf);
         }
         public void AddReportField(string Caption, Font HeaderFont, string Field, StringAlignment TextAlignment, Font DetailFont
                     , bool WordWrap, string Bottom, bool Sum, string FormatString, Size HeaderSize)
         { 
             DevExpress.XtraPrinting.TextAlignment alig = DevExpress.XtraPrinting.TextAlignment.MiddleLeft;
             if (TextAlignment == StringAlignment.Center) alig = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
             if (TextAlignment == StringAlignment.Far) alig = DevExpress.XtraPrinting.TextAlignment.MiddleRight;
             SetReportField(Caption, HeaderFont, Field, alig, DetailFont, WordWrap, Bottom, Sum, FormatString, HeaderSize);
         }
         /// <summary>
         /// 定义报表字段
         /// </summary>
         /// <param name="Caption">表格头标题</param>
         /// <param name="Field">绑定字段</param>
         public void AddReportField(string Caption, string Filed)
         {
             AddReportField(Caption, null, Filed, StringAlignment.Near, null, false, "", false, string.Empty, Size.Empty);
         }
         /// <summary>
         /// 定义条码
         /// </summary>
         /// <param name="Text">条码内容(A-Z,0-9)</param>
         /// <param name="pint">起点</param>
         /// <param name="size">大小</param>
         /// <param name="showText">是否显示文字</param>
         /// <param name="Field">字段名</param>
         public void SetReportBarCode(string Text, Point pint, Size size, bool showText, string Field)
         {
             m_rpb.Text = Text;
             m_rpb.pint = pint;
             m_rpb.size = size;
             m_rpb.showText = showText;
             m_rpb.Field = Field;            
         }        private const int c_LineWidth =3;//预留边线的宽度
         /// <summary>
         /// 报表每个单元打印前事件
         /// </summary>
         public event ReportUnitAfterPrint UnitAfterPrint;
         //报表头、报表尾整个报表中只会加载一次,页头、页尾每一页都会加载一次
         //本通用报表只有页头,页尾和表体三部份
         
         private ReportMain rpMain;
         /// <summary>
         /// 初始化通用表格打印
         /// </summary>
         public ctlTableXReport()
         {
             this.ReportUnit = ReportUnit.TenthsOfAMillimeter; //使用0.1毫米计量单位
             this.Dpi = 254F;
             base.BeforePrint += new System.Drawing.Printing.PrintEventHandler(XReport_BeforePrint);
             base.AfterPrint+=new EventHandler(XReport_AfterPrint);
              
         }
         /// <summary>
         /// 显示打印
         /// </summary>
         /// <returns></returns>
         public void  ShowDialog()
         {
             base.ShowPreview();
         }
         /// <summary>
         /// 显示打印
         /// </summary>
         /// <param name="owner"></param>
         /// <returns></returns>
         public void  ShowDialog(IWin32Window owner)
         {
             base.ShowPreviewDialog();
         }
         /// <summary>
         /// 自动页长时计算每页打印的行数
         /// </summary>
         /// <param name="rows">总行数</param>
         /// <param name="maxrow">充许每页打印最大行数</param>
         /// <returns></returns>
         public int GetAutoRow(int rows, int maxrow)
         {
             if (rows % maxrow == 0)
                 return maxrow;
             else
             {
                 //找余数为0或最接近最大行数
                 List<int> m = new List<int>();
                 for (int i = maxrow; i >= maxrow * 3 / 4; i--)
                 {
                     m.Add(rows % i);
                     if (rows % i == 0) return i;
                 }
                 int k = m.AsEnumerable().Max();
                 for (int i = 0; i < m.Count(); i++)
                 {
                     if (m[i] == k) return maxrow - i;//只能最接近了
                 }
                 return maxrow;
             }
         }
         /// <summary>
         /// 设置标准格式报表
         /// </summary>
         private  void SetReport( ReportMain  rpm,ReportTitle[] rpt,ReportField[] rpf ,ReportBarcode rpb)
         {             
            rpMain = rpm;           
            //初始化变量
            if (rpMain.Align == 0) rpMain.Align = StringAlignment.Center;
            for (int i = 0; i < rpt.Length; i++)
            {
                if (rpt[i].xTextAlignment ==0) rpt[i].xTextAlignment=DevExpress.XtraPrinting.TextAlignment.TopCenter;
                if (rpt[i].Font == null) rpt[i].Font = new System.Drawing.Font("楷体_GB2312", 14.25F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            }
            for (int i = 0;i < rpf.Length ;i++)
            {
                if (rpf[i].xTextAlignment ==0) rpf[i].xTextAlignment=DevExpress.XtraPrinting.TextAlignment.TopLeft;
                if (rpf[i].HeaderFont==null) rpf[i].HeaderFont = new System.Drawing.Font("楷体_GB2312", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                if (rpf[i].DetailFont==null) rpf[i].DetailFont = new System.Drawing.Font("楷体_GB2312", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
                if (rpf[i].HeaderSize.IsEmpty)
                {
                    SizeF size = MeasureString(rpf[i].Caption,rpf[i].HeaderFont);
                    rpf[i].HeaderSize = new Size((int)size.Width + (i==0? 3:2) * c_LineWidth, (int)size.Height + 30); //宽度增加2个线位,防止自动换行                  
                }
            }           if (rpm.Margins != null) this.Margins = rpm.Margins;
            if (!rpm.pageSize.IsEmpty)
            {
                this.PaperKind = System.Drawing.Printing.PaperKind.Custom;              
                this.PageSize = rpm.pageSize;
            }
            else
                this.PaperKind = rpm.PaperKind;
            this.Landscape = rpm.Landscape;
            if (rpm.printerName != string.Empty) this.PrinterName = rpm.printerName;            DetailBand detail = new DetailBand();
            PageHeaderBand pageHeader = new PageHeaderBand();
            PageFooterBand pageFooter = new PageFooterBand();
            pageHeader.Dpi = 254F;  //用于0.1毫米的计量
            detail.Dpi = 254F;
            pageFooter.Dpi = 254F;
            XRPanel TitleHeader = CreateTitleHeader(rpt, true);
            XRPanel TitleFooter = CreateTitleHeader(rpt, false);
            XRTable TableHeader = CreateTableHeader(rpf,rpMain.GridLine==ReportGridLine.Full);
            XRTable TableFooter = CreateTableFooter(rpf, rpm.DetailHeight, rpMain.GridLine == ReportGridLine.Full);
            XRTable TableDetail = CreateTableDetail(rpf, rpm.DetailHeight, rpMain.GridLine == ReportGridLine.Full);
            XRBarCode BarCode = createBarCode(rpb);
            //校正表格的对齐方式
            switch (rpMain.Align)
            {
                case  StringAlignment.Near:
                    TableHeader.Left = c_LineWidth;
                    break;
                case StringAlignment.Far:
                    TableHeader.Left = (this.PageWidth - (this.Margins.Left + this.Margins.Right)) - TableHeader.Width - c_LineWidth;
                    break;
                default:  //中间
                    TableHeader.Left = (this.PageWidth - (this.Margins.Left + this.Margins.Right) - TableHeader.Width) /2 ;
                    break;
            }
            TableDetail.Left = TableHeader.Left;
            TableFooter.Left = TableHeader.Left;           
            //页头 
            TableHeader.Top = c_LineWidth + TitleHeader.Top + TitleHeader.Height ;
            if (rpb.Text !=null && rpb.Text != string.Empty)  //加入条码
            {
                pageHeader.Controls.Add(BarCode);
                if (TableHeader.Top + TableHeader.Height < BarCode.Height) TableHeader.Top = BarCode.Height - TableHeader.Height;
            }                            
            pageHeader.Controls.Add(TitleHeader);
            pageHeader.Controls.Add(TableHeader);
            pageHeader.Height =  TableHeader.Top + TableHeader.Height;         
            //表格头二条线
            if (rpm.GridLine == ReportGridLine.Three || rpm.GridLine == ReportGridLine.Border)
            {
                pageHeader.Controls.Add(CreateLine(TableHeader.Left ,TableHeader.Top,TableHeader.Width,TableHeader.Height, DevExpress.XtraPrinting.BorderSide.Top));
                pageHeader.Controls.Add(CreateLine(TableHeader.Left ,TableHeader.Top,TableHeader.Width,TableHeader.Height, DevExpress.XtraPrinting.BorderSide.Bottom));
                //两侧线
                if (rpm.GridLine == ReportGridLine.Border)
                {
                    pageHeader.Controls.Add(CreateLine(TableHeader.Left, TableHeader.Top, TableHeader.Width, TableHeader.Height, DevExpress.XtraPrinting.BorderSide.Left));
                    pageHeader.Controls.Add(CreateLine(TableHeader.Left, TableHeader.Top, TableHeader.Width, TableHeader.Height, DevExpress.XtraPrinting.BorderSide.Right));
                }
            }           
            //带区
            detail.Height = rpm.DetailHeight;
            detail.Controls.Add(TableDetail);
            //两侧线
            if (rpm.GridLine == ReportGridLine.Border)
            {
                detail.Controls.Add(CreateLine(TableDetail.Left, TableDetail.Top, TableDetail.Width, TableDetail.Height, DevExpress.XtraPrinting.BorderSide.Left));
                detail.Controls.Add(CreateLine(TableDetail.Left, TableDetail.Top, TableDetail.Width, TableDetail.Height, DevExpress.XtraPrinting.BorderSide.Right));
            }
            //页脚
            if (rpm.GridLine == ReportGridLine.Three || rpm.GridLine == ReportGridLine.Border) //一条线
            {
                pageFooter.Controls.Add(CreateLine(TableDetail.Left, 0, TableDetail.Width, 0, DevExpress.XtraPrinting.BorderSide.Top));
                TableFooter.Top = c_LineWidth;
                TitleFooter.Top = c_LineWidth;
            }
            else
            {
                TableFooter.Top = 0;
                TitleFooter.Top = 0;
            }
            if (TableFooter.Rows[0].Cells.Count > 0)
            {               
                TitleFooter.Top = TableFooter.Height+TableFooter.Top;   
                pageFooter.Controls.Add(TableFooter);              
            }          
             pageFooter.Controls.Add(TitleFooter);
             if (rpm.DetailRows > 0)
             {
                 if (rpm.AutoPageHeight) //自动计算页长
                 {
                     int h = TitleFooter.Top + TitleFooter.Height + pageHeader.Height + detail.Height * rpm.DetailRows + this.Margins.Top + this.Margins.Bottom;
                     this.PaperKind = System.Drawing.Printing.PaperKind.Custom;
                     this.PageSize = new Size(this.PageWidth, h+1);
                 }
                 //计算余下的空白
                 if (pageHeader.Height + detail.Height * rpm.DetailRows + this.Margins.Top + this.Margins.Bottom > this.PageHeight)
                     throw new Exception("PageHeight不够大,请重新定义纸张大小,或减少DetailRows");
                 pageFooter.Height = this.PageHeight - pageHeader.Height - detail.Height * rpm.DetailRows - this.Margins.Top - this.Margins.Bottom - 1; //必须减1         
             }
             else
             {
                 pageFooter.Height = TitleFooter.Top + TitleFooter.Height;
                 //实际能打印行数然后校正表脚,另表格高度刚好整行数
                 int r = (this.PageHeight - pageHeader.Height - this.Margins.Top - this.Margins.Bottom - pageFooter.Height) / detail.Height;
                 pageFooter.Height = this.PageHeight - pageHeader.Height - detail.Height * r - this.Margins.Top - this.Margins.Bottom-1;                       
             }
            Bands.AddRange(new DevExpress.XtraReports.UI.Band[] { detail, pageHeader,pageFooter});
            //this.Bands[BandKind.PageHeader].Controls.Add(tableHeader); 另一种加入方法
                     
         }             // 创建表线(从指定距形中创建边线,线在边框外)
         //由于线条有一个宽度,起点坐标要向左移一个宽度,水平线还要增长2个宽度,上水平线向上移一个宽度
         private XRLine CreateLine(int x ,int y,int w,int h ,DevExpress.XtraPrinting.BorderSide BorderSide)
         {
             XRLine xrline = new XRLine();
             xrline.Dpi = 254F;
             xrline.LineDirection = (BorderSide == DevExpress.XtraPrinting.BorderSide.Left || BorderSide == DevExpress.XtraPrinting.BorderSide.Right) ? LineDirection.Vertical : LineDirection.Horizontal;            
             xrline.LineWidth = c_LineWidth; //线宽
             if (xrline.LineDirection == LineDirection.Vertical) //垂直
             {
                 xrline.Width = c_LineWidth;
                 //当LineWidth少于5时,Width或Height会自动校正为5,必须手工校正坐标
                 xrline.Left = x -(xrline.Width - c_LineWidth)/2+ (BorderSide == DevExpress.XtraPrinting.BorderSide.Right ? w : -c_LineWidth);  
                 xrline.Height = h;
                 xrline.Top = y;              
             }
             else //水平
             {
                 xrline.Height = c_LineWidth;
                 //当LineWidth少于5时,Width或Height会自动校正为5,必须手工校正坐标
                 xrline.Top = y -(xrline.Height - c_LineWidth)/2+(BorderSide == DevExpress.XtraPrinting.BorderSide.Bottom?h:-c_LineWidth);              
                 xrline.Width = w + 2* c_LineWidth; //水平线加一个宽度
                 xrline.Left = x - c_LineWidth;
             }
             return xrline;
         }
         
         #region 创建表格的各个功能单元
         // 创建表格头
         private XRTable CreateTableHeader(ReportField[] rpf,bool border)
         {
             System.Drawing.Size tablesize = new Size();             
             XRTableRow headerRow = new XRTableRow();
             headerRow.Dpi = 254F; 
             foreach (ReportField obj in rpf)
             {
                 XRTableCell headerCell = new XRTableCell();
                 headerCell.Dpi = 254F;
                 headerCell.Font = obj.HeaderFont;               
                 headerCell.Width = obj.HeaderSize.Width;
                 if (border)   //上下边和侧边
                     if (obj.Equals(rpf[0])) headerCell.Borders =(DevExpress.XtraPrinting.BorderSide) (DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Left| DevExpress.XtraPrinting.BorderSide.Bottom);
                     else  headerCell.Borders = (DevExpress.XtraPrinting.BorderSide)( DevExpress.XtraPrinting.BorderSide.Bottom| DevExpress.XtraPrinting.BorderSide.Top | DevExpress.XtraPrinting.BorderSide.Right );                                
                 headerCell.BorderWidth = c_LineWidth;
                 if (tablesize.Height < obj.HeaderSize.Height) tablesize.Height = obj.HeaderSize.Height;
                 headerCell.WordWrap = true;  //自动换行
                 headerCell.TextAlignment = DevExpress.XtraPrinting.TextAlignment.MiddleCenter;
                 headerCell.CanGrow = false;
                 headerCell.Text = obj.Caption;
                 headerCell.Tag = obj.Field;
                 //headerCell.BackColor = Color.Red;
                 headerCell.PrintOnPage += new PrintOnPageEventHandler(headerCell_PrintOnPage);
                 if (obj.Name !=null && obj.Name != string.Empty) headerCell.Name ="header_"+ obj.Name;
                 headerRow.Cells.Add(headerCell);
                 tablesize.Width = tablesize.Width + headerCell.Width;
             }             
             headerRow.Size = tablesize;
             XRTable tableHeader = new XRTable();
             tableHeader.Dpi = 254F;
             tableHeader.Size = tablesize;
             tableHeader.Rows.Add(headerRow);
             return tableHeader;
         }
         // 创建表格
         private XRTable CreateTableDetail(ReportField[] rpf, int rowHeight,bool border)
         {
             System.Drawing.Size tablesize = new Size();
             tablesize.Height = rowHeight;
             XRTableRow detailRow = new XRTableRow();
             detailRow.Dpi = 254F;            
             foreach (ReportField obj in rpf)
             {
                 XRTableCell detailCell = new XRTableCell();
                 detailCell.Dpi = 254F;
                 detailCell.Font = obj.DetailFont;
                 detailCell.Width = obj.HeaderSize.Width; 
                 if (obj.FormatString==null || obj.FormatString == string.Empty)
                     detailCell.DataBindings.Add("Text", null, obj.Field);
                 else 
                    detailCell.DataBindings.Add("Text", null,obj.Field,obj.FormatString);
                 detailCell.Tag  = obj.Caption;
                 detailCell.TextAlignment = obj.xTextAlignment;
                 detailCell.WordWrap = obj.WordWrap;
                 //detailCell.BackColor = Color.Blue;
                 detailCell.CanGrow = false;
                 if (border)   //侧边和下边
                     if (obj.Equals(rpf[0])) detailCell.Borders = (DevExpress.XtraPrinting.BorderSide)(DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Left| DevExpress.XtraPrinting.BorderSide.Bottom);
                     else detailCell.Borders = (DevExpress.XtraPrinting.BorderSide)(DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom);
                 detailCell.BorderWidth = c_LineWidth;
                 detailCell.PrintOnPage += new PrintOnPageEventHandler(detailCell_PrintOnPage);
                 if (obj.Name != null && obj.Name != string.Empty) detailCell.Name = "detail_" + obj.Name;
                 detailRow.Cells.Add(detailCell);               
                 tablesize.Width = tablesize.Width + detailCell.Width;// +LineCell.Width;
             }
             detailRow.Size = tablesize;
             XRTable tableDetail = new XRTable();
             tableDetail.Dpi = 254F;
             tableDetail.Size = tablesize;
             tableDetail.Rows.Add(detailRow);
             tableDetail.PrintOnPage += new PrintOnPageEventHandler(tableDetail_PrintOnPage);
             return tableDetail;
         }        // 创建表格脚(若Rows[0].Cells.Count 表示无表格脚)
         private XRTable CreateTableFooter(ReportField[] rpf, int rowHeight, bool border)
         {
             System.Drawing.Size tablesize = new Size();
             tablesize.Height = rowHeight;
             XRTableRow FooterRow = new XRTableRow();
             FooterRow.Dpi = 254F;
             bool isfooter = false;
             foreach (ReportField obj in rpf)
             {
                 if (obj.Sum || (obj.Bottom != null && obj.Bottom != string.Empty))
                 {
                     isfooter = true;
                     break;
                 }
             }
             if (isfooter)
                 foreach (ReportField obj in rpf)
                 {
                     XRTableCell FooterCell = new XRTableCell();
                     FooterCell.Dpi = 254F;
                     FooterCell.Font = obj.HeaderFont;
                     FooterCell.Width = obj.HeaderSize.Width;
                     FooterCell.Tag = obj.Caption;
                     FooterCell.CanGrow = false;
                     if (obj.Sum)  //合计
                     {
                         if (obj.FormatString == null || obj.FormatString == string.Empty)
                             FooterCell.DataBindings.Add("Text", null, obj.Field);
                         else
                             FooterCell.DataBindings.Add("Text", null, obj.Field, obj.FormatString);
                         DevExpress.XtraReports.UI.XRSummary xrSummary = new DevExpress.XtraReports.UI.XRSummary();
                         xrSummary.Running = DevExpress.XtraReports.UI.SummaryRunning.Page;
                         FooterCell.Summary = xrSummary;
                     }
                     else
                         FooterCell.Text = obj.Bottom;
                     if (border)   //侧边和下边
                         if (obj.Equals(rpf[0])) FooterCell.Borders = (DevExpress.XtraPrinting.BorderSide)(DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Left | DevExpress.XtraPrinting.BorderSide.Bottom);
                         else FooterCell.Borders = (DevExpress.XtraPrinting.BorderSide)(DevExpress.XtraPrinting.BorderSide.Right | DevExpress.XtraPrinting.BorderSide.Bottom);
                     FooterCell.BorderWidth = c_LineWidth;
                     FooterCell.TextAlignment = obj.xTextAlignment;
                     FooterCell.WordWrap = obj.WordWrap;
                     //FooterCell.BackColor = Color.Yellow;
                     FooterCell.PrintOnPage += new PrintOnPageEventHandler(FooterCell_PrintOnPage);
                     if (obj.Name != null && obj.Name != string.Empty) FooterCell.Name = "footer_" + obj.Name;
                     FooterRow.Cells.Add(FooterCell);
                     tablesize.Width = tablesize.Width + FooterCell.Width;// +LineCell.Width;                }
             XRTable tableFooter = new XRTable();
             tableFooter.Dpi = 254F;
             FooterRow.Size = tablesize;
             tableFooter.Size = tablesize;
             tableFooter.Rows.Add(FooterRow);
             return tableFooter;
         }
         // 创建页头或页脚标题 (Rows.Count =0表示无标题) 
         private XRPanel CreateTitleHeader(ReportTitle[] rpt, bool Header)
         {
             XRPanel xrpHeader = new XRPanel();
             xrpHeader.Dpi = 254F;
             System.Drawing.Size size = new Size();
             foreach (ReportTitle obj in rpt)
             {
                 if (obj.Header == Header)
                 {
                     XRLabel xrt = new XRLabel();
                     xrt.Dpi = 254F;
                     xrt.Text = obj.Caption;
                     xrt.Tag = Header;
                     xrt.TextAlignment = obj.xTextAlignment;
                     xrt.WordWrap = true;
                     xrt.CanGrow = false;
                     xrt.Font = obj.Font;
                     if (obj.size.IsEmpty)
                     {
                         xrt.Width = (this.PageWidth - (this.Margins.Left + this.Margins.Right));
                         xrt.Height = (int)MeasureString(obj.Caption, obj.Font, xrt.Width).Height + 1;
                     }
                     else
                     {
                         xrt.Width = obj.size.Width;
                         xrt.Height = obj.size.Height;
                     }
                     xrt.PrintOnPage += new PrintOnPageEventHandler(title_PrintOnPage);
                     xrt.Top = size.Height;
                     size.Width = xrt.Width;
                     size.Height = size.Height + xrt.Height; ;
                     xrpHeader.Controls.Add(xrt);
                 }
             }
             xrpHeader.Size = size;
             return xrpHeader;
         }        // 创建条码
         private XRBarCode createBarCode(ReportBarcode rpb)
         {
             XRBarCode xrbar = new XRBarCode();
             xrbar.AutoModule = true;
             xrbar.Dpi = 254F;
             xrbar.Location = rpb.pint.IsEmpty ? new Point(0, 0) : rpb.pint;
             xrbar.Module = 5.08F;
             xrbar.Size = rpb.size.IsEmpty ? new Size(this.PageWidth - this.Margins.Left - this.Margins.Right, 150) : rpb.size;
             xrbar.ShowText = rpb.showText;
             xrbar.Symbology = new DevExpress.XtraPrinting.BarCode.Code128Generator() ;
             xrbar.PrintOnPage += new PrintOnPageEventHandler(xrbar_PrintOnPage);
             if (!string.IsNullOrEmpty(rpb.Field))
                 xrbar.DataBindings.Add("Text", null, rpb.Field);
             else
                 xrbar.Text = rpb.Text;
             return xrbar;
         }           
         #endregion        #region   //处理表格各个事件
        //每页单元格事件
         void tableDetail_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             m_rowindex++;
         }        void headerCell_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             if (UnitAfterPrint == null) return;
             XRTableCell cell = (XRTableCell)sender;
             string value = cell.Text;
             UnitAfterPrint(sender,ReportBandType.TableHeader , e.PageIndex, m_rowindex, cell.Text, cell.Tag.ToString(), ref value, new EventArgs());
             cell.Text = value;
         }             //处理表格单元事件
         void detailCell_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             if (UnitAfterPrint == null) return;
             XRTableCell cell = (XRTableCell)sender;
             string field = "";
             string value = cell.Text;
             if (cell.DataBindings.Count > 0) field = cell.DataBindings[0].DataMember;                     
             UnitAfterPrint(sender, ReportBandType.TableDetail, e.PageIndex, m_rowindex, cell.Tag.ToString(), field, ref value , new EventArgs());
             cell.Text = value;
         }        //处理标题单元事件
         void title_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             if (UnitAfterPrint == null) return;
             XRLabel xrl = (XRLabel)sender;
             string value = xrl.Text;
             UnitAfterPrint(sender, Convert.ToBoolean(xrl.Tag) ? ReportBandType.PageHeader  : ReportBandType.PageFooder, e.PageIndex, m_rowindex, xrl.Text, "", ref value , new EventArgs());
             xrl.Text = value;
         }          
         //表格脚事件
         void FooterCell_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             if (UnitAfterPrint == null) return;
             XRTableCell cell = (XRTableCell)sender;
             string field = "";
             string value = cell.Text;
             if (cell.DataBindings.Count > 0) field = cell.DataBindings[0].PropertyName;
             UnitAfterPrint(sender, ReportBandType.TableFooter , e.PageIndex, m_rowindex, cell.Tag.ToString(), field, ref value, new EventArgs());
             cell.Text = value;
         }
         //条码事件
         void xrbar_PrintOnPage(object sender, PrintOnPageEventArgs e)
         {
             if (UnitAfterPrint == null) return;
             XRBarCode barcode = (XRBarCode)sender;
             string value = barcode.Text;
             UnitAfterPrint(sender, ReportBandType.BarCode , e.PageIndex, m_rowindex,barcode.Text,"", ref value, new EventArgs());
             barcode.Text = value;
         } 
         #endregion        void XReport_BeforePrint(object sender, System.Drawing.Printing.PrintEventArgs e)
         {
             m_rowindex = 0;
         }
         void XReport_AfterPrint(object sender, EventArgs e)
         {
             for (int i = this.PrintingSystem.Pages.Count; i > 0; i--)
             {
                 if ((i < m_rpm.BeginPage && m_rpm.BeginPage > 0) || (i > m_rpm.EndPage && m_rpm.EndPage > 0))
                     this.PrintingSystem.Pages.Remove(this.PrintingSystem.Pages[1]); //移去指定的页码;
             }
         }
         //测量字符的打印范围(单行)
         private SizeF MeasureString(string text, Font font)
         {
             return MeasureString(text, font, 10000);
         }
         //测量字符的打印范围(多行)
         private SizeF MeasureString(string text, Font font,int width)
         {
             //Graphics fg = CreateGraphics();
             //fg.PageUnit = GraphicsUnit.Pixel;
             //fg.MeasureString();
            SizeF size=DevExpress.XtraPrinting.BrickGraphics.MeasureString(text, font, width, null, GraphicsUnit.Millimeter);
            size.Width = size.Width * 10;
            size.Height = size.Height * 10;
            return size;
         }
            }
 }