← 返回所有博客文章

在表格中插入汇总信息是常见的重要数据展示方式。本文介绍如何在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); 
}

 

示例下载