public override void SetValue(ISheet sheet, JToken token) { JArray data; if (this.Each == "$root") { data = (JArray)token; } else { data = (JArray)token[Each]; } var row = sheet.GetRow(RowIndex); var grouprows = ExcelGroupRow.GetGroupRows(data, TreeCode); for (int i = 0; i < data.Count; i++) { var item = data[i]; foreach (var cellTemplate in Cells) { var cell = row.GetCell(cellTemplate.ColIndex); var val = item[cellTemplate.Bind]; cellTemplate.SetCellValue(cell, val); } if (i != data.Count - 1) { row = sheet.CopyRow(row.RowNum, row.RowNum + 1); } } var groups = grouprows.Values.OrderByDescending(o => o.TreeCode).ToList(); var b = JsonConvert.SerializeObject(groups); foreach (var grouprow in groups) { sheet.GroupRow(grouprow.StartIndex + RowIndex + 1, grouprow.EndIndex + RowIndex); } if (data.Count == 0) { sheet.ShiftRows(row.RowNum + 1, sheet.LastRowNum, -1); } }
public static Dictionary <string, ExcelGroupRow> GetGroupRows(JArray array, string treecodeFiled) { var dict = new Dictionary <string, ExcelGroupRow>(); if (string.IsNullOrEmpty(treecodeFiled)) { return(dict); } for (int i = 0; i < array.Count; i++) { var item = array[i]; var treecode = (string)item[treecodeFiled]; treecode = treecode.Trim(); ExcelGroupRow row = new ExcelGroupRow(); row.StartIndex = i; row.EndIndex = i; row.TreeCode = treecode; var parentCode = treecode.Substring(0, Math.Max(0, treecode.LastIndexOf('.'))); while (!string.IsNullOrEmpty(parentCode)) { ExcelGroupRow parent; if (dict.TryGetValue(parentCode, out parent)) { row.Parent = parent; row.Level = parent.Level + 1; item[treecodeFiled] = new string(' ', 2 * row.Level) + treecode; while (parent != null) { parent.EndIndex = i; parent = parent.Parent; } break; } parentCode = parentCode.Substring(0, Math.Max(0, parentCode.LastIndexOf('.') - 1)); } dict.Add(treecode, row); } return(dict); }
public static Dictionary<string, ExcelGroupRow> GetGroupRows(JArray array, string treecodeFiled) { var dict = new Dictionary<string, ExcelGroupRow>(); if (string.IsNullOrEmpty(treecodeFiled)) return dict; for (int i = 0; i < array.Count; i++) { var item = array[i]; var treecode = (string)item[treecodeFiled]; treecode = treecode.Trim(); ExcelGroupRow row = new ExcelGroupRow(); row.StartIndex = i; row.EndIndex = i; row.TreeCode = treecode; var parentCode = treecode.Substring(0, Math.Max(0, treecode.LastIndexOf('.'))); while (!string.IsNullOrEmpty(parentCode)) { ExcelGroupRow parent; if (dict.TryGetValue(parentCode, out parent)) { row.Parent = parent; row.Level = parent.Level + 1; item[treecodeFiled] = new string(' ', 2 * row.Level) + treecode; while (parent != null) { parent.EndIndex = i; parent = parent.Parent; } break; } parentCode = parentCode.Substring(0, Math.Max(0, parentCode.LastIndexOf('.') - 1)); } dict.Add(treecode, row); } return dict; }