gpt4 book ai didi

c# - 嵌套表

转载 作者:太空宇宙 更新时间:2023-11-03 11:07:37 28 4
gpt4 key购买 nike

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

最佳答案

对表示层和数据层代码进行必要的重构,如下所示:

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 的开发人员布道师。

关于c# - 嵌套表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15222963/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com