zoukankan      html  css  js  c++  java
  • C# winform 动态构建fastreport报表

     private void DoPrint()
            {
                DataView dv = (DataView)dgv_apply_details.DataSource;
                Report report = new Report();
                //给report注册数据源
                report.RegisterData(dv, "Purchasment");
                //设置能在报表中使用
                report.GetDataSource("Purchasment").Enabled = true;
                ReportPage page1 = new ReportPage();
                page1.Name = "Page1";
                page1.SetDefaults();
    
                //设置标题
                page1.ReportTitle = new ReportTitleBand();
                page1.ReportTitle.Name = "ReportTitle1";
                page1.ReportTitle.Height = 0;//Units.Centimeters * 0.1f;
                //给标题赋值
                TextObject txtTitle = new TextObject();
                txtTitle.Name = "TextTitle";
                txtTitle.Bounds = new RectangleF(0, 0, Units.Centimeters * 19, Units.Centimeters * 1);
                txtTitle.Text = "药品采购汇总单";
                txtTitle.HorzAlign = HorzAlign.Center;
                txtTitle.VertAlign = VertAlign.Center;
                txtTitle.Font = new Font("微软雅黑", 20, FontStyle.Bold);
                page1.ReportTitle.Objects.Add(txtTitle);
    
                GroupHeaderBand group1 = new GroupHeaderBand();
                group1.Name = "GroupHeader1";
                group1.Height = Units.Centimeters * 2f;
                group1.Condition = "[Purchasment.order_id]";//分组条件
    
                DataBand data1 = new DataBand();
                data1.Name = "Data1";
                data1.Height = Units.Centimeters * 1.5f;//Units.Centimeters * 3f;//调的是data里面数据(即dataHeaderBand)的间距,也是table中row之间的间距
                data1.DataSource = report.GetDataSource("Purchasment");
    
                #region 分组表头
                //GetDataBand(ref dataBand, ref dataHeaderBand);
                //TextObject txt2 = new TextObject();
                //txt2.Name = "Text2";
                //txt2.Bounds = new RectangleF(0, 0, Units.Centimeters * 20, Units.Centimeters * 3);
                //txt2.VertAlign = VertAlign.Center;
                //txt2.Font = new Font("宋体", 9, FontStyle.Bold);
                //txt2.Text = dataHeaderBand;//"订单号:[Purchasment.order_id]";
                //group1.Objects.Add(txt2);
    
                //TextObject txt3 = new TextObject();
                //txt3.Name = "Text3";
                //txt3.Bounds = new RectangleF(Units.Centimeters * 8, 0, Units.Centimeters * 20, Units.Centimeters * 3);
                //txt3.VertAlign = VertAlign.Center;
                //txt3.Text = "申请药房:[Purchasment.storage_name]";
                //group1.Objects.Add(txt3);
    
                ////<TextObject Name="Text38" Left="198.45" Top="18.9" Width="75.6" Height="28.35" Text="订单状态:" VertAlign="Center"/>
                //TextObject txt4 = new TextObject();
                //txt4.Name = "Text4";
                ////txt4.Bounds = new RectangleF(float.Parse("198.45"), float.Parse("18.9"), float.Parse("75.6"), float.Parse("28.35"));
                //txt4.Bounds = new RectangleF(Units.Centimeters * 15, 0, Units.Centimeters * 10, Units.Centimeters * 1);
                //txt4.VertAlign = VertAlign.Center;
                //txt4.Text = "采购科室:[Purchasment.sourceStorage]";
                //group1.Objects.Add(txt4);
                #endregion
                //DataHeaderBand dataHeader1 = new DataHeaderBand();
                //dataHeader1.Name = "DataHeader1";
                //dataHeader1.Height = Units.Centimeters * 1;
                //data1.Objects.Add(dataHeader1);
                //GetDataBand(ref dataBand, ref dataHeaderBand);
    
                //TextObject txt6 = new TextObject();
                //txt6.Name = "Text6";
                //txt6.Bounds = new RectangleF(0, Units.Centimeters * 2, Units.Centimeters * 30, Units.Centimeters * 1);
                //txt6.Text = dataHeaderBand;
                //txt6.Font = new Font("宋体", 9, FontStyle.Bold);
                //dataHeader1.Objects.Add(txt6);
                ////group1.Objects.Add(txt6);
    
                //TextObject txt7 = new TextObject();
                //txt7.Name = "Text7";
                //txt7.Bounds = new RectangleF(0, Units.Centimeters * 3, Units.Centimeters * 30, Units.Centimeters * 1f);
                //txt7.Text = dataBand;
                //txt7.Font = new Font("宋体", 9);
                //data1.Objects.Add(txt7);
    
                #region Table表格
                TableObject table1 = new TableObject();
                table1.Name = "Table1";
                table1.Border.Lines = BorderLines.All;
                table1.Height = Units.Centimeters * 1.5f;
                //table1.Width = Units.Centimeters * 30;
    
                #region TableColumn
                //TableColumn column1 = new TableColumn();
                //column1.Name = "Column1";
                //column1.Width = Units.Centimeters * 3;
                //table1.AddChild(column1);//AddChild应为添加子节点,适用于table内部
                ////table1.ChildObjects.Add(column);//试过这种添加方式,不过这样就不显示表格了
    
                //TableColumn column2 = new TableColumn();
                //column2.Name = "Column2";
                //column2.Width = Units.Centimeters * 3;
                //table1.AddChild(column2);
    
                //TableColumn column3 = new TableColumn();
                //column3.Name = "Column3";
                //column3.Width = Units.Centimeters * 3;
                //table1.AddChild(column3);
    
                //TableColumn column4 = new TableColumn();
                //column4.Name = "Column4";
                //column4.Width = Units.Centimeters * 3;
                //table1.AddChild(column4);
    
                //TableColumn column5 = new TableColumn();
                //column5.Name = "Column5";
                //column5.Width = Units.Centimeters * 3;
                //table1.AddChild(column5);
    
                //TableColumn column6 = new TableColumn();
                //column6.Name = "Column6";
                //column6.Width = Units.Centimeters * 3;
                //table1.AddChild(column6); 
                #endregion
    
                TableRow row1 = new TableRow();
                row1.Name = "Row1";
                row1.Height = Units.Centimeters * 1.5f;
                table1.AddChild(row1);
    
    
                #region TableCell cell1
                //TableCell cell1 = new TableCell();
                //cell1.Name = "Cell1";
                //cell1.HorzAlign = HorzAlign.Center;
                //cell1.VertAlign = VertAlign.Center;
                //cell1.Border.Lines = BorderLines.All;
                //cell1.Text = "[Purchasment.drug_name]";
                //cell1.Font = new Font("宋体", 9);
                //row1.AddChild(cell1);
    
                //TableCell cell2 = new TableCell();
                //cell2.Name = "Cell2";
                //cell2.HorzAlign = HorzAlign.Center;
                //cell2.VertAlign = VertAlign.Center;
                //cell2.Border.Lines = BorderLines.All;
                //cell2.Text = "[Purchasment.package_spec]";
                //cell2.Font = new Font("宋体", 9);
                //row1.AddChild(cell2);
    
                //TableCell cell3 = new TableCell();
                //cell3.Name = "Cell3";
                //cell3.HorzAlign = HorzAlign.Center;
                //cell3.VertAlign = VertAlign.Center;
                //cell3.Border.Lines = BorderLines.All;
                //cell3.Text = "[Purchasment.sale_price]";
                //cell3.Font = new Font("宋体", 9);
                //row1.AddChild(cell3);
    
                //TableCell cell4 = new TableCell();
                //cell4.Name = "Cell4";
                //cell4.HorzAlign = HorzAlign.Center;
                //cell4.VertAlign = VertAlign.Center;
                //cell4.Border.Lines = BorderLines.All;
                //cell4.Text = "[Purchasment.manufacture_name]";
                //cell4.Font = new Font("宋体", 9);
                //row1.AddChild(cell4);
    
                //TableCell cell5 = new TableCell();
                //cell5.Name = "Cell5";
                //cell5.HorzAlign = HorzAlign.Center;
                //cell5.VertAlign = VertAlign.Center;
                //cell5.Border.Lines = BorderLines.All;
                //cell5.Text = "[Purchasment.distributor_name]";
                //cell5.Font = new Font("宋体", 9);
                //row1.AddChild(cell5);
    
                //TableCell cell6 = new TableCell();
                //cell6.Name = "Cell6";
                //cell6.HorzAlign = HorzAlign.Center;
                //cell6.VertAlign = VertAlign.Center;
                //cell6.Border.Lines = BorderLines.All;
                //cell6.Text = "[Purchasment.amount]";
                //cell6.Font = new Font("宋体", 9);
                //row1.AddChild(cell6); 
                #endregion
    
                SetDataHeaderAndTable(group1, page1.PaperWidth, table1, row1);
    
                data1.Objects.Add(table1);
    
    
                #endregion
    
                #region 报表底
                page1.PageFooter = new PageFooterBand();
                page1.PageFooter.Name = "PageFooter1";
                page1.PageFooter.Height = Units.Centimeters * 1;//这个高度必须>=PageFooter包含文字的RectangleF里面的y的值
    
                TextObject ftxt1 = new TextObject();
                ftxt1.Name = "FText1";
                ftxt1.Bounds = new RectangleF(0, Units.Centimeters * 1, Units.Centimeters * 10, Units.Centimeters * 1);
                ftxt1.Text = "打印时间:[Date]";
                ftxt1.Font = new Font("宋体", 9);
                ftxt1.VertAlign = VertAlign.Center;
                page1.PageFooter.Objects.Add(ftxt1);
    
                TextObject ftxt2 = new TextObject();
                ftxt2.Name = "FText2";
                ftxt2.Bounds = new RectangleF(Units.Centimeters * 15, Units.Centimeters * 1, Units.Centimeters * 10, Units.Centimeters * 1);
                ftxt2.Text = "页码:[PageN]/[TotalPages#]";
                ftxt2.Font = new Font("宋体", 9);
                ftxt2.VertAlign = VertAlign.Center;
                page1.PageFooter.Objects.Add(ftxt2);
    
                TextObject ftxt3 = new TextObject();
                ftxt3.Name = "FText3";
                ftxt3.Bounds = new RectangleF(Units.Centimeters * 8, Units.Centimeters * 1, Units.Centimeters * 10, Units.Centimeters * 1);
                ftxt3.Text = "打印人:[operator]";
                ftxt3.Font = new Font("宋体", 9);
                ftxt3.VertAlign = VertAlign.Center;
                page1.PageFooter.Objects.Add(ftxt3);
                report.SetParameterValue("operator", SysConfig.UserCode);
                #endregion
    
                //group1.Objects.Add(data1);
                group1.Data = data1;
                page1.Bands.Add(group1);
                report.Pages.Add(page1);
                report.Show();
            }
    /// <summary>
            /// 根据当前显示的字段,动态添加数据首和表格
            /// </summary>
            private void SetDataHeaderAndTable(GroupHeaderBand group1, float pageWidth, TableObject table1, TableRow row1)
            {
                int count = 0;//记录显示的列数
                int index = 0;
                DataTable dt = OrderBL.SelectFreecombination(this.Name);
                if (dt != null)
                {
                    for (int i = 0; i < dgv_apply_details.Columns.Count; i++)
                    {
                        if (dgv_apply_details.Columns[i].Visible)
                        {
                            count++;
                        }
                    }
                    if (count <= 0)
                    {
                        YxMessageBox.ShowWarning("当前没有可打印的数据!");
                        return;
                    }
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (DbHelper.GetBool(dt.Rows[i], "print_status"))
                        {
                            TextObject text = new TextObject();
                            text.Name = "Text" + index.ToString();
                            text.Bounds = new RectangleF(0 + index * (3 * pageWidth / count), 0, 3 * pageWidth / count, Units.Centimeters * 3);
                            text.VertAlign = VertAlign.Center;
                            text.Font = new Font("宋体", 9, FontStyle.Bold);
                            text.Text = DbHelper.GetString(dt.Rows[i], "header_text");
                            group1.Objects.Add(text);
                            
                            TableColumn column1 = new TableColumn();
                            column1.Name = "Column" + index.ToString(); ;
                            column1.Width = 3 * pageWidth / count;//Units.Centimeters * 3;
                            table1.AddChild(column1);
    
                            TableCell cell1 = new TableCell();
                            cell1.Name = "Cell" + index.ToString(); ;
                            cell1.HorzAlign = HorzAlign.Center;
                            cell1.VertAlign = VertAlign.Center;
                            cell1.Border.Lines = BorderLines.All;
                            cell1.Text = "[Purchasment." + DbHelper.GetString(dt.Rows[i], "dataproperty_name") + "] ";
                            cell1.Font = new Font("宋体", 9);
                            row1.AddChild(cell1);
    
                            index++;
                        }
                    }
                }
            }

    打印效果如图:

     

  • 相关阅读:
    IntelliJ IDEA 14.03 java 中文文本处理中的编码格式设置
    应聘感悟
    STL string分析
    CUDA SDK VolumeRender 分析 (1)
    BSP
    CUDA SDK VolumeRender 分析 (3)
    CUDA SDK VolumeRender 分析 (2)
    Windows软件发布时遇到的一些问题
    Ten Commandments of Egoless Programming (转载)
    复习下光照知识
  • 原文地址:https://www.cnblogs.com/wsn1203/p/12054520.html
Copyright © 2011-2022 走看看