2

为ASPOSE.Words做研究。一切正常,只剩下最后一件事。问题是如何在表格中渲染表格?在嵌套表文档中,示例数据与表示层紧密耦合。我需要分离数据和表示层。这里的测试很少:

[Test]
public void CreateDocumentRecurentalTableInTableTest()
{
  // Structural items are in [], values/data in {}
  //GIVEN (presentation layer)
  const string FileName = "_6CreateDocumentRecurentalTableInTableTest.txt";
  var doc = new Document();
  var builder = new DocumentBuilder(doc);
  builder.Writeln("TEST -- START");

  builder.InsertField(@"MERGEFIELD TableStart:[MyTable] MERGEFORMAT");

  builder.InsertField(@"MERGEFIELD [MyTableCol1] \* MERGEFORMAT");
  builder.InsertField(@"MERGEFIELD [MyTableCol2] \* MERGEFORMAT");

  builder.InsertField(@"MERGEFIELD TableStart:[SubTable] MERGEFORMAT");

  builder.InsertField(@"MERGEFIELD [SubTable.Col1] \* MERGEFORMAT");
  builder.InsertField(@"MERGEFIELD [SubTable.Col2] \* MERGEFORMAT");

  builder.InsertField(@"MERGEFIELD TableEnd:[SubTable] MERGEFORMAT");
  builder.InsertField(@"MERGEFIELD TableEnd:[MyTable] MERGEFORMAT");

  builder.Writeln("\nTEST -- END");

  //WHEN (Data layer)
  using (var dt2 = new DataTable("[SubTable]"))
  {
      dt2.Columns.Add("[SubTable.Col1]");
      dt2.Columns.Add("[SubTable.Col2]");

      dt2.Rows.Add(" {SubTable.Row1.Cont1} ", " {SubTable.Row1.Cont2} ");
      dt2.Rows.Add(" {SubTable.Row2.Cont1} ", " {SubTable.Row2.Cont2} ");
      dt2.Rows.Add(" {SubTable.Row3.Cont1} ", " {SubTable.Row3.Cont2} ");

      using (var dt = new DataTable("[MyTable]"))
      {
          dt.Columns.Add("[MyTableCol1]");
          dt.Columns.Add("[MyTableCol2]");

          dt.Rows.Add(" {MyTable.firstRow} ", dt2);

          doc.MailMerge.ExecuteWithRegions(dt);
          doc.Save(FileName, SaveFormat.Text);
      }
  }

  //THEN
  //Assert...
}

得到结果:

TEST -- START
{MyTable.firstRow} [SubTable]«TableStart:[SubTable]»«[SubTable.Col1]»«[SubTable.Col2]»«TableEnd:[SubTable]»
TEST -- END

我想要达到的目标:

TEST -- START
 {MyTable.firstRow}  {SubTable.Row1.Cont1}  {SubTable.Row1.Cont2} 
                     {SubTable.Row2.Cont1}  {SubTable.Row2.Cont2} 
                     {SubTable.Row3.Cont1}  {SubTable.Row3.Cont2} 
 {MyTable.nextRow}  
TEST -- END

如果两个表都存储在 DataSet.Tables 中,那么它会为每个表呈现多次:

TEST -- START
 {MyTable.firstRow}  {SubTable.Row1.Cont1}  {SubTable.Row1.Cont2} 
 {MyTable.firstRow}  {SubTable.Row2.Cont1}  {SubTable.Row2.Cont2} 
 {MyTable.firstRow}  {SubTable.Row3.Cont1}  {SubTable.Row3.Cont2} 
 {MyTable.nextRow}  {SubTable.Row1.Cont1}  {SubTable.Row1.Cont2} 
 {MyTable.nextRow}  {SubTable.Row2.Cont1}  {SubTable.Row2.Cont2} 
 {MyTable.nextRow}  {SubTable.Row3.Cont1}  {SubTable.Row3.Cont2} 
TEST -- END
4

2 回答 2

2

对表示层和数据层代码执行所需的重构,如下所示:

public void CreateDocumentRecurentalTableInTableTest()
{

// Structural items are in [], values/data in {}
//GIVEN (presentation layer)
//const string FileName = "_6CreateDocumentRecurentalTableInTableTest.txt";

var doc = new Document();
var builder = new DocumentBuilder(doc);

builder.Writeln("TEST -- START");
builder.InsertField(@"MERGEFIELD TableStart:[MyTable] MERGEFORMAT");
builder.InsertField(@"MERGEFIELD [MyTableCol1] \* MERGEFORMAT");
builder.InsertField(@"MERGEFIELD [MyTableCol2] \* MERGEFORMAT");
builder.Writeln();

builder.InsertField(@"MERGEFIELD TableStart:[SubTable] MERGEFORMAT");
builder.InsertField(@"MERGEFIELD [SubTable.Col1] \* MERGEFORMAT");
builder.InsertField(@"MERGEFIELD [SubTable.Col2] \* MERGEFORMAT");
builder.InsertField(@"MERGEFIELD TableEnd:[SubTable] MERGEFORMAT");
builder.Writeln();

builder.InsertField(@"MERGEFIELD TableEnd:[MyTable] MERGEFORMAT");

builder.Writeln("\nTEST -- END");

//WHEN (Data layer)
DataSet ds = new DataSet();

var dt = new DataTable("[MyTable]");
dt.Columns.Add("[MyTableCol1]");
dt.Columns.Add("[MyTableCol2]");
dt.Columns.Add("[Id]");

dt.Rows.Add(" {MyTable.firstRow} ", "", 0);
dt.Rows.Add(" {MyTable.nextRow} ", "", 1);

var dt2 = new DataTable("[SubTable]");
dt2.Columns.Add("[SubTable.Col1]");
dt2.Columns.Add("[SubTable.Col2]");
dt2.Columns.Add("[Id]");

dt2.Rows.Add(" {SubTable.Row1.Cont1} ", " {SubTable.Row1.Cont2} ", 0);
dt2.Rows.Add(" {SubTable.Row2.Cont1} ", " {SubTable.Row2.Cont2} ", 0);
dt2.Rows.Add(" {SubTable.Row3.Cont1} ", " {SubTable.Row3.Cont2} ", 0);

ds.Tables.Add(dt);
ds.Tables.Add(dt2);

ds.Relations.Add("MyRelation", dt.Columns[2], dt2.Columns[2], true);

doc.MailMerge.CleanupOptions = MailMergeCleanupOptions.RemoveUnusedRegions | MailMergeCleanupOptions.RemoveEmptyParagraphs;

doc.MailMerge.ExecuteWithRegions(ds);

doc.Save(@"C:\Temp\\out.docx");

//THEN

//Assert...
}

这会提供与您想要的完全相同的输出,例如:

TEST -- START
 {MyTable.firstRow} 
  {SubTable.Row1.Cont1}  {SubTable.Row1.Cont2} 
  {SubTable.Row2.Cont1}  {SubTable.Row2.Cont2} 
  {SubTable.Row3.Cont1}  {SubTable.Row3.Cont2} 
 {MyTable.nextRow} 
TEST -- END

我叫 Iqbal,是 Aspose 的开发人员布道师。

于 2013-04-01T11:02:17.720 回答
0

简而言之:必须链接数据表(如 RDBMS)。

简而言之:在此处阅读更多信息

于 2013-03-07T15:59:15.693 回答