把代码备份一下,免得硬盘又坏了,看来已经造成心理阴影了啊。
方式一:
//清单范本 public void test1() { //生成说明 var ds = ExcelHelper.ExcelToDataSet(true, "清单范本.xlsx"); var dt = ds.Tables[0]; dt.Columns.Remove("序号"); //列名 List<string> columnNames = new List<string>(); PropertyInfo[] pArray = typeof(ListingTemplate).GetProperties(); pArray.ToList().ForEach(c => { columnNames.Add(c.Name); }); //Excel的列名 修改为 属性名 for (int i = 0; i < dt.Columns.Count; i++) { dt.Columns[i].ColumnName = columnNames[i]; } List<ListingTemplate> allListingTemplates = new List<ListingTemplate>(); //三级节点 List<ListingTemplate> threeNodes = new List<ListingTemplate>(); threeNodes = DataTableToEntities<ListingTemplate>(dt); //一级节点 ListingTemplate oneNode = new ListingTemplate { Id = 1, Level = 1, Name = "重庆奉溪高速公路" }; allListingTemplates.Add(oneNode); //二级节点 int twoNodes_StartId = allListingTemplates.OrderByDescending(c => c.Id).FirstOrDefault().Id; var twoNodes = threeNodes.Where(c => !string.IsNullOrWhiteSpace(c.Chapter)).Select(c => { twoNodes_StartId++; return new ListingTemplate { Id = twoNodes_StartId, Chapter = c.Chapter, Name = c.Chapter, Level = 2, ParentId = oneNode.Id }; }); allListingTemplates.AddRange(twoNodes); //三级节点 生成树 //根据清单编码生成 判断依据 含有“-”的数量 和 含有“(”的数量 、 名称是否以父节点Code一个开头 int threeNodes_StartId = allListingTemplates.OrderByDescending(c => c.Id).FirstOrDefault().Id; for (int i = 0; i < threeNodes.Count; i++) { var item = threeNodes[i]; threeNodes_StartId++; item.Id = threeNodes_StartId; bool hasBracket = item.Code.Contains("("); int num = item.Code.Split(new string[] { "-" }, StringSplitOptions.None).Length - 1; if ((num == 0) && !string.IsNullOrWhiteSpace(item.Chapter)) { item.Level = 3; item.ParentId = allListingTemplates.Where(c => c.Name == item.Chapter).FirstOrDefault().Id; } else if (num == 0) { item.Level = 4; } if ( (num == 1) && hasBracket && item.Code.StartsWith(//当前编码 以 上级编码 开始 allListingTemplates .Where(c => c.Level == 5) .OrderByDescending(c => c.Id) .FirstOrDefault() .Code ) ) { item.Level = 6; } //else if () //{ // //有一个“-”,上面个是章节 //} else if (num == 1) { item.Level = 5; } if (num == 2) { item.Level = 6; } if (num == 3) { item.Level = 7; } if (num == 4) { item.Level = 8; } //else if ((num == 3) || ((num == (num - 1)) && hasBracket)) //{ // item.Level = 7; //} //else if ((num == 4) || ((num == (num - 1)) && hasBracket)) //{ // item.Level = 8; //} //else if ((num == 5) || ((num == (num - 1)) && hasBracket)) //{ // item.Level = 9; //} if (num > 4) { throw new Exception("暂无该判断!" + item.Code); } if (item.Level >= 4) { item.ParentId = allListingTemplates .Where(c => c.Level == (item.Level - 1)) .OrderByDescending(c => c.Id) .FirstOrDefault().Id; } allListingTemplates.Add(item); } StringBuilder sb = new StringBuilder(); allListingTemplates.ForEach(c => { var sql = GetInsertSQL<ListingTemplate>(c); sb.Append(sql); }); ExecuteNonQuery(sb.ToString()); }
方式二:
//清单范本 public void test1_1() { //生成说明 var ds = ExcelHelper.ExcelToDataSet(true, "清单范本-原版2.xlsx"); var dt = ds.Tables[0]; dt.Columns.Remove("序号"); //列名 List<string> columnNames = new List<string>(); PropertyInfo[] pArray = typeof(ListingTemplate).GetProperties(); pArray.ToList().ForEach(c => { columnNames.Add(c.Name); }); //Excel的列名 修改为 属性名 for (int i = 0; i < dt.Columns.Count; i++) { dt.Columns[i].ColumnName = columnNames[i]; } List<ListingTemplate> allListingTemplates = new List<ListingTemplate>(); //三级节点 List<ListingTemplate> threeNodes = new List<ListingTemplate>(); threeNodes = DataTableToEntities<ListingTemplate>(dt); //一级节点 ListingTemplate oneNode = new ListingTemplate { Id = 1, Level = 1, Name = "重庆奉溪高速公路" }; allListingTemplates.Add(oneNode); //二级节点 章节不为空 int twoNodes_StartId = allListingTemplates.OrderByDescending(c => c.Id).FirstOrDefault().Id; var twoNodes = threeNodes.Where(c => !string.IsNullOrWhiteSpace(c.Chapter)).Select(c => { twoNodes_StartId++; return new ListingTemplate { Id = twoNodes_StartId, Chapter = c.Chapter, Name = c.Chapter, Level = 2, ParentId = oneNode.Id }; }); allListingTemplates.AddRange(twoNodes); //汇总为父节点 没有汇总的为子节点 //三级节点 生成树 //根据清单编码生成 判断依据 含有“-”的数量 和 含有“(”的数量 、 名称是否以父节点Code一个开头 int threeNodes_StartId = allListingTemplates.OrderByDescending(c => c.Id).FirstOrDefault().Id; for (int i = 0; i < threeNodes.Count; i++) { var item = threeNodes[i]; threeNodes_StartId++; item.Id = threeNodes_StartId; //判断逻辑: //第一个汇总 怎么关联父节点:第一个汇总前面的汇总 即为父节点 //第二个汇总 怎么关联父节点:往上找同级汇总节点 找到连续的两个汇总节点,且后一个是前一个的汇总子节点 ListingTemplate parent = null; if ((item.Type == "汇总") && !string.IsNullOrWhiteSpace(item.Chapter)) { //汇总 含 章节 parent = allListingTemplates.Where(c => c.Name == item.Chapter).First(); } else if (!string.IsNullOrWhiteSpace(item.Chapter)) { //不是汇总 但 含 章节 parent = allListingTemplates.Where(c => c.Name == item.Chapter).First(); } else if (item.Type == "汇总") { //汇总 不含 章节 if (allListingTemplates.Last().Type == "汇总") { //判断上一个是否也是汇总 parent = allListingTemplates.Last(); } else { if (!item.Code.Contains("-")) { //如果没有包含‘-’,父节点 为 含章节的节点 parent = allListingTemplates.Where(c => !string.IsNullOrWhiteSpace(c.Chapter)).Last(); } else { //倒序查找汇总节点 var list = allListingTemplates.Where(c => c.Type == "汇总").ToList(); for (int j = list.Count - 1; j >= 0; j--) { if (j == 0) { break; } //相邻判断:id相减等于一 var item1 = list[j]; var item2 = list[j - 1]; if ((item1.Id - item2.Id) == 1) { //相邻 且 同级 if (item1.Code.Split('-').Length == item.Code.Split('-').Length) { parent = item2; break; } } } //上面没有找到 那就用编码去找 if (parent == null) { parent = allListingTemplates.Where(c => c.Code == item.Code.Substring(0, item.Code.LastIndexOf('-'))).First(); } } } } else { //子节点 parent = allListingTemplates.Where(c => c.Type == "汇总").Last(); } item.ParentId = parent.Id; item.Level = parent.Level + 1; allListingTemplates.Add(item); } StringBuilder sb = new StringBuilder(); allListingTemplates.ForEach(c => { var sql = GetInsertSQL<ListingTemplate>(c); sb.Append(sql); }); ExecuteNonQuery(sb.ToString()); }