From b01e2aa84e1b95b4af5188693f1a0881ea7f13a9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E5=BD=AC?= <1759414356@qq.com> Date: Tue, 10 Feb 2026 12:16:06 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E6=89=A9=E5=B1=95=E6=B8=B2=E6=9F=93table?= =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=8D=95=E5=85=83=E6=A0=BC=E6=B8=B2=E6=9F=93?= =?UTF-8?q?=EF=BC=8C=E5=8D=95=E5=85=83=E6=A0=BC=E6=A8=A1=E6=9D=BF{{}}?= =?UTF-8?q?=E5=8F=AA=E6=9C=89=E4=B8=80=E4=B8=AA=E7=9A=84=E6=97=B6list?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E6=B8=B2=E6=9F=93=E6=AF=8F=E4=B8=AA=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=A0=BC=EF=BC=8C=E8=B6=85=E8=BF=87=E4=B8=80=E4=B8=AA?= =?UTF-8?q?=E6=97=B6=E6=B8=B2=E6=9F=93=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MiniWord/MiniWord.Implment.cs | 52 ++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 7 deletions(-) diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index 6e48862..af712c7 100644 --- a/src/MiniWord/MiniWord.Implment.cs +++ b/src/MiniWord/MiniWord.Implment.cs @@ -81,6 +81,7 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum ReplaceText(xmlElement, docx, tags); } + /// /// 渲染Table /// @@ -91,6 +92,9 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum private static void GenerateTable(Table table, WordprocessingDocument docx, Dictionary tags) { var trs = table.Descendants().ToArray(); // remember toarray or system will loop OOM; + var regexStr = "(?<={{).*?\\..*?(?=}})"; + //计算是否只有一个cell存在指令,如果超过1个则纵向渲染,否则横向渲染。 + bool isHorizontal = table.Elements().SelectMany(s => s.Elements()).Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1; foreach (var tr in trs) { @@ -98,8 +102,9 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict .Replace("{{if(", "").Replace(")if", "").Replace("endif}}", ""); // 匹配list数据,格式“Items.PropName” - var matchs = (Regex.Matches(innerText, "(?<={{).*?\\..*?(?=}})") + var matchs = (Regex.Matches(innerText, regexStr) .Cast().GroupBy(x => x.Value).Select(varGroup => varGroup.First().Value)).ToArray(); + if (matchs.Length > 0) { //var listKeys = matchs.Select(s => s.Split('.')[0]).Distinct().ToArray(); @@ -124,12 +129,17 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict var attributeKey = matchs[0].Split('.')[0]; var list = tagObj as IEnumerable; + int num = tr.Elements().Count(); + int index = 0; + List elementList = new List(); + + foreach (var item in list) { var dic = new Dictionary(); //TODO: optimize - var newTr = tr.CloneNode(true); + if (item is IDictionary) { var es = (Dictionary)item; @@ -153,16 +163,43 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict ReplaceIfStatements(newTr, tags: dic); ReplaceText(newTr, docx, tags: dic); - //Fix #47 The table should be inserted at the template tag position instead of the last row - if (table.Contains(tr)) + + if (isHorizontal) { - table.InsertBefore(newTr, tr); + var newTable = newTr.Descendants().FirstOrDefault(); + if (newTable != null) + elementList.Add(newTable.CloneNode(true)); + + if (index > 0 && (index + 1) % num == 0 && elementList.Count > 0) + { + var templateTr = tr.CloneNode(true); + for (var i = 0; i < elementList.Count; i++) + { + var tCell = templateTr.Elements().ToList()[i]; + + tCell.RemoveAllChildren(); + tCell.Append(elementList[i]); + } + newTr = templateTr; + elementList = new List(); + + table.Append(newTr); + } } else { - // If it is a nested table, temporarily append it to the end according to the original plan. - table.Append(newTr); + //Fix #47 The table should be inserted at the template tag position instead of the last row + if (table.Contains(tr)) + { + table.InsertBefore(newTr, tr); + } + else + { + // If it is a nested table, temporarily append it to the end according to the original plan. + table.Append(newTr); + } } + index++; } tr.Remove(); @@ -194,6 +231,7 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict } + /// /// 获取Obj对象指定的值 /// From aae9d7c6adc2061d8c737d845bf32fc2ec9b19e8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E5=BD=AC?= <1759414356@qq.com> Date: Tue, 10 Feb 2026 17:48:55 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20=E5=BD=93=E6=B8=B2?= =?UTF-8?q?=E6=9F=93table=E6=97=B6=E4=BD=BF=E7=94=A8=20Descendants=20?= =?UTF-8?q?=E4=BC=9A=E8=87=AA=E5=8A=A8=E6=8F=90=E5=8F=96=E6=89=80=E6=9C=89?= =?UTF-8?q?=E7=9A=84tableRow,=E5=8C=85=E6=8B=AC=E5=B5=8C=E5=A5=97=E7=9A=84?= =?UTF-8?q?tableRow,=E6=94=B9=E7=94=A8Elements=EF=BC=8C=E5=8F=AA=E6=8F=90?= =?UTF-8?q?=E5=8F=96=E5=AD=90=E7=BA=A7=E7=9A=84tr=E3=80=82=20=E4=B8=8D?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=95=88=E6=9E=9C=E6=98=AF=E5=87=BA=E7=8E=B0?= =?UTF-8?q?=E5=A4=A7=E9=87=8F=E7=9A=84=E6=97=A0=E6=95=88tr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MiniWord/MiniWord.Implment.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index af712c7..3a2fd02 100644 --- a/src/MiniWord/MiniWord.Implment.cs +++ b/src/MiniWord/MiniWord.Implment.cs @@ -91,7 +91,7 @@ private static void Generate(this OpenXmlElement xmlElement, WordprocessingDocum /// private static void GenerateTable(Table table, WordprocessingDocument docx, Dictionary tags) { - var trs = table.Descendants().ToArray(); // remember toarray or system will loop OOM; + var trs = table.Elements().ToArray(); // remember toarray or system will loop OOM; var regexStr = "(?<={{).*?\\..*?(?=}})"; //计算是否只有一个cell存在指令,如果超过1个则纵向渲染,否则横向渲染。 bool isHorizontal = table.Elements().SelectMany(s => s.Elements()).Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1; From 83afbe8c9d1fb76cff7ee66693a41c434506bc3c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=AF=85=E5=BD=AC?= <1759414356@qq.com> Date: Tue, 10 Feb 2026 17:59:24 +0800 Subject: [PATCH 3/3] =?UTF-8?q?=E4=BF=AE=E5=A4=8Dcell=E6=95=B0=E9=87=8F?= =?UTF-8?q?=E8=AE=A1=E7=AE=97=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MiniWord/MiniWord.Implment.cs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index 3a2fd02..05dd45e 100644 --- a/src/MiniWord/MiniWord.Implment.cs +++ b/src/MiniWord/MiniWord.Implment.cs @@ -94,7 +94,8 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict var trs = table.Elements().ToArray(); // remember toarray or system will loop OOM; var regexStr = "(?<={{).*?\\..*?(?=}})"; //计算是否只有一个cell存在指令,如果超过1个则纵向渲染,否则横向渲染。 - bool isHorizontal = table.Elements().SelectMany(s => s.Elements()).Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1; + var cellList = table.Elements().SelectMany(s => s.Elements()).ToList(); + bool isHorizontal = cellList.Count > 1 && cellList.Count(w => Regex.Matches(w.InnerText, regexStr).Count > 0) <= 1; foreach (var tr in trs) {