在C1PrintDocument中显示汇总数据

在表格中插入汇总信息是常见的重要数据展示方式。本文介绍如何在C1PrintDocument的RenderTable中添加汇总信息并打印出来。<div style="width:400px;height:200px;overflow:hidden"><img src="http://our.componentone.com/wp-content/uploads/2014/01/Result.png" style="float:none; margin-top: -150px;margin-left: -140px;" /></div>

发布于 2014/02/14 00:00

ComponentOne Enterprise

在表格中插入汇总信息是常见的重要数据展示方式。本文介绍如何在C1PrintDocument的RenderTable中添加汇总信息并打印出来。

 

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

 

实现办法主要是在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<int, int>)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<int, int>(); 
    _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<int, int>)e.Page.Document.UserData).Add(e.Page.Index, summ); 
}

 

示例下载

ComponentOne Enterprise | 下载试用

ComponentOne 是一套专注于企业 .NET开发、支持 .NET Core 平台,并完美集成于 Visual Studio 的第三方控件集,包含 300 多种 .NET开发控件,提供表格数据管理、数据可视化、报表和文档、日程安排、输入和编辑、导航和布局、系统提升工具等七大功能,被誉为“.NET开发的‘瑞士军刀’”。

ComponentOne 为您提供专业的产品咨询服务,并由技术支持工程师为您1对1解答。>> 发帖提问

相关产品
推荐相关案例
关注微信
葡萄城社区二维码

关注“葡萄城社区”

加微信获取技术资讯

加微信获取技术资讯

想了解更多信息,请联系我们, 随时掌握技术资源和产品动态