为ASPOSE.Words做研究。一切都工作正常,最后一件事仍然存在。问题是如何在表格内部渲染表格?在Nested table文档中,示例数据与表示层紧密耦合。我需要分离数据和表示层。这里的测试很少:
[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
答案 0 :(得分:1)
对表示层和数据层代码执行所需的重新分解,如下所示:
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的开发人员传播者。
答案 1 :(得分:0)
简而言之:必须链接数据表(如RDBMS)。
简而言之:Read more here。