2014-11-25 63 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"); 
    }