diff --git a/src/MiniWord/MiniWord.Implment.cs b/src/MiniWord/MiniWord.Implment.cs index 6e48862..05dd45e 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 /// @@ -90,7 +91,11 @@ 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个则纵向渲染,否则横向渲染。 + 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) { @@ -98,8 +103,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 +130,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 +164,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 +232,7 @@ private static void GenerateTable(Table table, WordprocessingDocument docx, Dict } + /// /// 获取Obj对象指定的值 ///