1、测试环境
.Net8,Aspose.Words(v24.11.1)
2、代码
using System.Text;
using Aspose.Words;
using Aspose.Words.Reporting;
Console.WriteLine("Start");
var jsonData = """
{
"testA": [
[
{ "name": "User1" },
{ "name": "User2" }
],
[
{ "name": "User3" },
{ "name": "User4" }
]
],
"testB": [
[
1,
2
],
[
3,
4
]
]
}
""";
var doc = new Document("Test.docx");
var dataSource = new JsonDataSource(new MemoryStream(Encoding.UTF8.GetBytes(jsonData)));
var engine = new ReportingEngine()
{
Options = ReportBuildOptions.AllowMissingMembers
};
engine.BuildReport(doc, dataSource, "data");
doc.Save($"{Guid.NewGuid()}.docx");
Console.WriteLine("End");
3、Word模板
testA的结果:
<<foreach [item in testA]>>
<<foreach [m in item]>><<[m.name]>>
<</foreach>><</foreach>>
testB的结果:
<<foreach [item in testB]>>
<<foreach [m in item]>><<[m_text]>>
<</foreach>><</foreach>>
4、问题
不知道我在Word模板中的绑定语法没有写对还是其他问题?导致出现如下错误:
5、需求
需要实现二维数组的循环绑定,也就是在foreach中嵌套foreach,同时JSON数据源的格式为上述JSON的格式。