2014-11-25 41 views
0

我目前需要一个想法,我正在开发一个软件解决方案,并且我必须从BDD的信息中创建Words文档。Aspose.Word/C上的报告系统#

一切都很好,大约信息的用途,但有一点,它是最重要的:

在我的Word文档,我用纹章重复其中的信息写入表。其中一列是指成本,而且由于我使用的是signets的系统,我不知道我的表格结束的位置,它可能需要1页,因为它可能需要2500页,但我需要在每页的末尾计算总共写入当前页面的每一行,然后在下一页的开头重写该总数。因此,这将是这样的:

nameOfTheItem1 Cost1 
nameOfTheItem2 Cost2 
nameOfTheItem3 Cost3 
nameOfTheItem4 Cost4 
TOTAL PAGE 1 : TotalPage1 
  • 第2页

TotalPage1

nameOfTheItem5 Cost5
nameOfTheItem6 Cost6
nameOfTheItem7 Cost7
nameOfTheItem8 Cost8
TOTAL PAGE 2: TotalPage2(+ TotalPage1)

,这为现有的用于该文档的每一页。

我仍然在寻找自己的解决方案,每一个帮助将是伟大的。

回答

0

可以使用Aspose.Words的mail merge功能完成。该解决方案特别与nested mail merge有关。

  1. 您必须将数据安排在DataSet中,以便根据您的要求显示报告。对于这种情况,安排在2个表格中。一个是“页面”,另一个是“项目”
  2. 您的模板文档(DOCX)应根据下图定义合并字段。请注意,表格后面有分页符。

enter image description here

下面的代码将帮助您上手。它当然使用虚拟数据。您可以填充自己的数据以使其适用于您。

MS Word模板文件,与此代码:Download Template

private void yourMethod() 
{ 
    string srcDoc = dataDir + "ItemsTemplate.docx"; 
    string dstDoc = dataDir + "ItemsTemplate_Result.docx"; 

    int totalRecords = 10; 
    int recordsPerPage = 4; 

    // Prepare some data 
    DataSet ds = getData(totalRecords, recordsPerPage); 

    // Prepare the document in Aspose 
    Aspose.Words.Document doc = new Aspose.Words.Document(srcDoc); 
    doc.MailMerge.ExecuteWithRegions(ds); 
    doc.MailMerge.CleanupOptions = Aspose.Words.Reporting.MailMergeCleanupOptions.RemoveEmptyParagraphs; 
    doc.Save(dstDoc); 

    Process.Start(dstDoc); 
} 

private DataSet getData(int totalRecords, int recordsPerPage) 
{ 
    DataSet ds = new DataSet("Dataset"); 

    // Add the page table 
    System.Data.DataTable pageTable = new System.Data.DataTable("Page"); 
    pageTable.Columns.Add("PageNumber"); 
    pageTable.Columns.Add("PageTotal"); 
    pageTable.Columns.Add("PreviousPageTotal"); 

    // Add the item table 
    System.Data.DataTable itemTable = new System.Data.DataTable("Item"); 
    itemTable.Columns.Add("ID"); 
    itemTable.Columns.Add("Name"); 
    itemTable.Columns.Add("Cost"); 
    itemTable.Columns.Add("PageNumber"); 

    // Add pages 
    int iRow = 1, iPage = 1; 
    while (iRow <= totalRecords) 
    { 
     DataRow pageRow = pageTable.NewRow(); 
     pageRow["PageNumber"] = iPage; 
     pageRow["PageTotal"] = 0; 

     // Add the items in this page 
     int iRecordsPerPage = 1; 
     while (iRow <= totalRecords && iRecordsPerPage <= recordsPerPage) 
     { 
      DataRow itemRow = itemTable.NewRow(); 
      itemRow["ID"] = iRow; 
      itemRow["Name"] = "Item " + iRow; 
      itemRow["Cost"] = iRow; 
      itemRow["PageNumber"] = iPage; 

      pageRow["PageTotal"] = int.Parse(pageRow["PageTotal"].ToString()) + int.Parse(itemRow["Cost"].ToString()); 

      itemTable.Rows.Add(itemRow); 
      iRow++; 
      iRecordsPerPage++; 
     } 

     pageTable.Rows.Add(pageRow); 

     // Previous page total 
     if (iPage == 1) 
      pageRow["PreviousPageTotal"] = 0; // Always 0 for first page 
     else 
      pageRow["PreviousPageTotal"] = pageTable.Rows[iPage - 2]["PageTotal"]; // Get total of previous page 

     iPage++; 
    } 

    ds.Tables.Add(pageTable); 
    ds.Tables.Add(itemTable); 

    // We must have relationship for Aspose mail merge to work correctly 
    ds.Relations.Add(pageTable.Columns["PageNumber"], itemTable.Columns["PageNumber"]); 

    return ds; 
} 

试着改变总记录和recordsPerPage变量的值,你会看到相应的安排在页上的数据。只要确保保持recordsPerPage值低,以便它不超过单个页面。

我是Aspose的开发人员传道人。

+0

谢谢,我会试试这个! – Kiskoul 2014-11-26 15:58:03

0

我正在将数据导出到现有文字模板,即.dotx 但即使我已将其添加到代码中,最终报告也不显示目录表。我的代码如下

public void ExportToWordUsingTemplate() 
    { 

     Aspose.Words.Document doc1 = new Aspose.Words.Document(@"E:/excel/HOVEDMAL Prognoserapporter 2.dotx"); 
     DocumentBuilder docBuilder1 = new DocumentBuilder(doc1); 

     SkinAPI.ReportAPISoapClient svc = new SkinAPI.ReportAPISoapClient(); 
     SkinAPI.GetReportContextResult myReportContext = svc.GetReportContext(1); 

     docBuilder1.InsertHtml("<h1 align='left'>" + myReportContext[0].MainReportName + "</h1>"); 
     docBuilder1.InsertTableOfContents("\\o \"1-3\" \\h \\z \\u"); 


     //for (int i = 0; i < myReportContext.Count - 2; i++) 
     for (int i = 0; i < 5; i++) 
     { 
      SkinAPI.GetReportElementGraphDataResult myElementGraphData = svc.GetReportElementGraphData(myReportContext[i].ReportId, myReportContext[i].ElementId); 
      SkinAPI.GetReportElementDataResult myElementData = svc.GetReportElementData(myReportContext[i].ReportId, myReportContext[i].ElementId, 0, 0, 0); // Three last parameters set to 0, used when fetching drilldown data for tables that support it 

      docBuilder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.Heading1; 
      docBuilder1.Writeln(myReportContext[i].ElementHeader); 
      docBuilder1.ParagraphFormat.StyleIdentifier = StyleIdentifier.BodyText; 

      // Is there a graph for this element, and has it a datasource other than the main data source as fetched above? 
      if (myReportContext[i].HasGraph && myReportContext[i].SeparateGraphDataSource) 
      { 
       // Is there a text part for this element 
       if (myReportContext[i].HasText) 
       { 
        // The returned string will contain a HTML text. 
        // Note that the text is connected to a TileId, not an ElementId, meening the text might have been fetched before. 
        string myElementHTMLDescription = svc.GetReportText(myReportContext[i].TileId); 
        docBuilder1.InsertHtml(myElementHTMLDescription);       
       } 
      } 
      docBuilder1.InsertBreak(BreakType.PageBreak); 
     } 

     doc1.Save(@"E:/excel/HOVEDMAL Prognoserapporter 2_Report.doc"); 
    }