趣百科

ComponentOne C1PrintDocument中显示汇总数据

编辑:Simone 2025-02-24 23:39:49 586 阅读

ComponentOne C1PrintDocument中显示汇总数据

如何在C1PrintDocument的RenderTable中添加汇总信息并打印出来。

PS:最近项目中使用 ComponentOne 控件,有些问题需要进行自定义和深入研究才能解决,看到百度经验中关于控件使用的分享也比较少。还是共享出来吧。

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

实现办法主要是在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.Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);

rt.Cols.Width = new Unit(5, C1.C1Preview.UnitTypeEnum.Cm);

rt.Cols.Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);

rt.Cols.Width = new Unit(2, C1.C1Preview.UnitTypeEnum.Cm);

rt.Cols.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);

}

版权声明:本站【趣百科】文章素材来源于网络或者用户投稿,未经许可不得用于商用,如转载保留本文链接:https://www.qubaik.com/life/143103.html

相关推荐