多语言展示
当前在线:1792今日阅读:26今日分享:39

ComponentOne C1PrintDocument中显示汇总数据

方法/步骤
1

以产品明细为例,我们需要在每一个页面中插入汇总项并显示在文档中。这个用例开起来有些棘手,不过实现起来其实很简单。在给出解决方案之前,先看看输出效果:

2

实现办法主要是在PageAdded事件中通过Dictionary存储汇总数据的计算结果,然后显示在页面中的最后一行。下面是代码片段:private void Generate() {    RenderTable rt = new RenderTable();    rt.Style.FontSize = 14;    rt.CellStyle.Spacing.All = new Unit(2, C1.C1Preview.UnitTypeEnum.Mm);    rt.Style.GridLines.All = new C1.C1Preview.LineDef(new Unit(0.3, UnitTypeEnum.Mm), Color.Black, DashStyle.Solid);    rt.Cols[0].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);    rt.Cols[1].Width = new Unit(5, C1.C1Preview.UnitTypeEnum.Cm);    rt.Cols[2].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);    rt.Cols[3].Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);    rt.Cols[4].Width = new Unit(3, C1.C1Preview.UnitTypeEnum.Cm);    // Header    int row = rt.Rows.Count;    rt.Cells[row, 0].Text = 'No.';    rt.Cells[row, 1].Text = 'Description';    rt.Cells[row, 2].Text = 'Count';    rt.Cells[row, 3].Text = 'Price';    rt.Cells[row, 4].Text = 'Sum';    rt.Rows[row].Style.FontBold = true;    rt.Rows[row].Style.BackColor = Color.LightGray;    rt.RowGroups[row, 1].PageHeader = true;    // Rows    int total = 0;    for (int n = 1; n < 64; n++)    {      row = rt.Rows.Count;      rt.Cells[row, 0].Text = string.Format('{0}', n);      rt.Cells[row, 1].Text = string.Format('Name {0}', n);      rt.Cells[row, 2].Text = string.Format('{0}', n * 2);      rt.Cells[row, 3].Text = string.Format('{0}', 1000);      CellData cellData;      cellData.value = 1000 * n * 2;      rt.Cells[row, 4].Text = string.Format('{0}', cellData.value);      rt.Cells[row, 4].Tag = cellData;      total += 1000 * n * 2;     }     // Page footer     row = rt.Rows.Count;     rt.Rows[row].Style.BackColor = Color.LightGray;     rt.Cells[row, 1].Text = 'PAGE TOTAL';     rt.Cells[row, 4].Text = '[((Dictionary)Page.Document.UserData)[Page.Index]]';     rt.Cells[row, 4].Style.TextColor = Color.Red;     rt.RowGroups[row, 1].PageFooter = true;     // Report footer     row = rt.Rows.Count;     rt.Rows[row].Style.BackColor = Color.Gray;     rt.Cells[row, 1].Text = 'GRAND TOTAL';     rt.Cells[row, 4].Text = string.Format('{0}', total);     rt.RowGroups[row, 1].Footer = TableFooterEnum.None;     _printDocument.UserData = new Dictionary();     _printDocument.Body.Children.Clear();     _printDocument.PageLayout.PageSettings.Landscape = true;     _printDocument.PageLayout.PageSettings.PaperKind = System.Drawing.Printing.PaperKind.A4;     _printDocument.Body.Children.Add(rt);     _printDocument.Generate(); } private void _printDocument_PageAdded(C1PrintDocument sender, PageEventArgs e) {     int summ = 0;     foreach (RenderFragment fragment in e.Page.Fragments)     {       foreach (object child in fragment.Children)       {         if (child.GetType() == typeof(RenderTextFragment))          {            RenderText text = ((RenderTextFragment)child).RenderObject;            if (text.TableCell.Tag != null && text.TableCell.Tag.GetType() == typeof(CellData))            {              summ += ((CellData)text.TableCell.Tag).value;            }           }         }      }      ((Dictionary)e.Page.Document.UserData).Add(e.Page.Index, summ); }

推荐信息