private static List <Classify> MakeTreeData() { List <Classify> tree = new List <Classify>(); Classify node_1 = new Classify { ClassifyName = "酒水饮料", ClassifyCode = "01", ClassfyLevel = 1 }; Classify node_1_1 = new Classify { ClassifyName = "白酒", ClassifyCode = "0101", ClassfyLevel = 2 }; Classify node_1_2 = new Classify { ClassifyName = "红酒", ClassifyCode = "0102", ClassfyLevel = 2 }; Classify node_1_3 = new Classify { ClassifyName = "啤酒", ClassifyCode = "0103", ClassfyLevel = 2 }; Classify node_1_1_1 = new Classify { ClassifyName = "茅台", ClassifyCode = "010101", ClassfyLevel = 3 }; Classify node_1_1_2 = new Classify { ClassifyName = "五粮液", ClassifyCode = "010102", ClassfyLevel = 3 }; Classify node_1_2_1 = new Classify { ClassifyName = "法国", ClassifyCode = "010201", ClassfyLevel = 3 }; Classify node_1_2_2 = new Classify { ClassifyName = "澳大利亚", ClassifyCode = "010202", ClassfyLevel = 3 }; Classify node_1_3_1 = new Classify { ClassifyName = "黑啤", ClassifyCode = "010301", ClassfyLevel = 3 }; Classify node_1_3_2 = new Classify { ClassifyName = "黄啤", ClassifyCode = "010302", ClassfyLevel = 3 }; node_1_1.Children = new List <Classify> { node_1_1_1, node_1_1_2 }; node_1_2.Children = new List <Classify> { node_1_2_1, node_1_2_2 }; node_1_3.Children = new List <Classify> { node_1_3_1, node_1_3_2 }; node_1.Children = new List <Classify> { node_1_1, node_1_2, node_1_3 }; tree.Add(node_1); Classify node_2 = new Classify { ClassifyName = "食品生鲜", ClassifyCode = "02", ClassfyLevel = 1 }; Classify node_2_1 = new Classify { ClassifyName = "新鲜水果", ClassifyCode = "0201", ClassfyLevel = 2 }; Classify node_2_2 = new Classify { ClassifyName = "蔬菜蛋品", ClassifyCode = "0202", ClassfyLevel = 2 }; Classify node_2_3 = new Classify { ClassifyName = "海鲜水产", ClassifyCode = "0203", ClassfyLevel = 2 }; Classify node_2_1_1 = new Classify { ClassifyName = "苹果", ClassifyCode = "020101", ClassfyLevel = 3 }; Classify node_2_1_2 = new Classify { ClassifyName = "菠萝", ClassifyCode = "020102", ClassfyLevel = 3 }; Classify node_2_2_1 = new Classify { ClassifyName = "西红柿", ClassifyCode = "020201", ClassfyLevel = 3 }; Classify node_2_2_2 = new Classify { ClassifyName = "包菜", ClassifyCode = "020202", ClassfyLevel = 3 }; Classify node_2_3_1 = new Classify { ClassifyName = "虾", ClassifyCode = "020301", ClassfyLevel = 3 }; Classify node_2_3_2 = new Classify { ClassifyName = "蟹", ClassifyCode = "020302", ClassfyLevel = 3 }; node_2_1.Children = new List <Classify> { node_2_1_1, node_2_1_2 }; node_2_2.Children = new List <Classify> { node_2_2_1, node_2_2_2 }; node_2_3.Children = new List <Classify> { node_2_3_1, node_2_3_2 }; node_2.Children = new List <Classify> { node_2_1, node_2_2, node_2_3 }; tree.Add(node_2); return(tree); }
private static void CreateTreeTable(Document doc, DocumentBuilder builder, List <Classify> tree) { Table table = builder.StartTable(); #region 表头 string[] titles = new string[] { "序号", "大类名称", "大类编码", "小类名称", "小类编码", "细类名称", "细类编码" }; int[] lens = new int[] { 4, 16, 16, 16, 16, 16, 16 }; for (int i = 0; i < 7; i++) { builder.InsertCell(); //插入单元格 builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[i]); //列宽-百分比 builder.CellFormat.Shading.BackgroundPatternColor = Color.LightGray; //背景色-灰色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐-居中 builder.Write(titles[i]); //写入内容 } builder.EndRow(); //结束行 #endregion int index = 1; //序号 for (int i = 0; i < tree.Count; i++) { Classify node1 = tree[i]; int num1 = 0; for (int j = 0; j < node1.Children.Count; j++) { Classify node2 = node1.Children[j]; int num2 = 0; for (int k = 0; k < node2.Children.Count; k++) { Classify node3 = node2.Children[k]; builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[0]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 builder.Write(index.ToString()); //写入内容 builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[1]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 if (num1 == 0) { builder.Write(node1.ClassifyName);//写入内容 builder.CellFormat.VerticalMerge = CellMerge.First; } else { builder.CellFormat.VerticalMerge = CellMerge.Previous; } builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[2]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 if (num1 == 0) { builder.Write(node1.ClassifyCode);//写入内容 builder.CellFormat.VerticalMerge = CellMerge.First; } else { builder.CellFormat.VerticalMerge = CellMerge.Previous; } builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[3]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 if (num2 == 0) { builder.Write(node2.ClassifyName);//写入内容 builder.CellFormat.VerticalMerge = CellMerge.First; } else { builder.CellFormat.VerticalMerge = CellMerge.Previous; } builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[4]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 if (num2 == 0) { builder.Write(node2.ClassifyCode);//写入内容 builder.CellFormat.VerticalMerge = CellMerge.First; } else { builder.CellFormat.VerticalMerge = CellMerge.Previous; } builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[5]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(node3.ClassifyName);//写入内容 builder.InsertCell(); builder.CellFormat.PreferredWidth = PreferredWidth.FromPercent(lens[6]); //列宽 builder.CellFormat.Shading.BackgroundPatternColor = Color.White; //背景色 builder.ParagraphFormat.Alignment = ParagraphAlignment.Center; //对齐 builder.CellFormat.VerticalMerge = CellMerge.None; builder.CellFormat.HorizontalMerge = CellMerge.None; builder.Write(node3.ClassifyCode);//写入内容 builder.EndRow(); index++; num1++; num2++; } } } #region 内容 #endregion builder.EndTable(); }