/// <summary> /// 生成word /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { //获取组合目录的节点个数 int count = treeView2.Nodes.Count; if (count == 0) { MessageBox.Show("组合目录中未有章节!"); return; } List <LeaderNode> leaderNodes = new List <LeaderNode>(); foreach (TreeNode node in treeView2.Nodes) { LeaderNode leaderNode = new LeaderNode(); leaderNode.Name = node.Text; if (node.Nodes.Count != 0) { List <CentreNode> centreNodes = new List <CentreNode>(); foreach (TreeNode node1 in node.Nodes) { CentreNode centreNode = new CentreNode(); centreNode.Name = node1.Text; if (node1.Nodes.Count != 0) { List <LastNode> lastNodes = new List <LastNode>(); foreach (TreeNode node2 in node1.Nodes) { LastNode lastNode = new LastNode(); lastNode.Name = node2.Text; lastNodes.Add(lastNode); } centreNode.LastNodeList = lastNodes; } centreNodes.Add(centreNode); } leaderNode.CentreNodeList = centreNodes; } leaderNodes.Add(leaderNode); } CreateWord createWord = CreateWord.GetInstance(leaderNodes); //CreateWord cw = new CreateWord(); createWord.Create(); }
/// <summary> /// 创建章节 /// </summary> /// <param name="doc"></param> private void CreateChaptersAndSections(XWPFDocument doc) { using (var myEntity = new MyEntity()) { //读取模板 FileStream stream = File.OpenRead(System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "模板\\施工说明书(模板).docx"); XWPFDocument readDoc = new XWPFDocument(stream); //创建段落 XWPFParagraph paragraph = doc.CreateParagraph(); paragraph.CreateRun().AddBreak();//新建页 for (int i = 0; i < leaderNodes.Count; i++) { LeaderNode leaderNode = leaderNodes[i]; paragraph = doc.CreateParagraph(); //创建段落 //行距 paragraph.setSpacingBetween(30, LineSpacingRule.EXACT); //固定值,30磅 //间距 paragraph.SpacingAfterLines = 8; //上 paragraph.SpacingBeforeLines = 8; //下 //对齐方式 paragraph.Alignment = ParagraphAlignment.BOTH; //两端对齐 //大纲级别 CT_DecimalNumber cT_DecimalNumber = new CT_DecimalNumber(); cT_DecimalNumber.val = "0级";//1级目录 paragraph.GetCTP().AddNewPPr().outlineLvl = cT_DecimalNumber; //标题信息 XWPFRun run = paragraph.CreateRun(); run.SetText((i + 1).ToString()); //1级 run.FontSize = 14; //字体大小 run.FontFamily = "Times New Roman"; //字体 run = paragraph.CreateRun(); run.SetText(" " + leaderNode.Name); //1级 run.FontSize = 14; //字体大小 run.FontFamily = "宋体"; //字体 //根据节点信息在数据库中查询 var se = (from s in myEntity.Section where s.name == leaderNode.Name select s).ToList(); Section section = se.First();//章节对象 //判断章节中是否存在内容 bool flag = IsContainTable(leaderNode.Name, doc, readDoc); if (flag) { //根据节点对象id信息在数据库内容表中查询 var contents = (from c in myEntity.ContentTab where c.section_id == section.Id select c).ToList(); if (contents.Count > 0) { foreach (ContentTab contentTab in contents) { paragraph = doc.CreateParagraph(); //创建段落 paragraph.IndentationFirstLine = MainForm.Indentation("宋体", 14, 4, new FontStyle()); //首行缩进 XWPFRun r = paragraph.CreateRun(); r.SetText(contentTab.content); r.FontFamily = "宋体"; r.FontSize = 14; } } } if (leaderNode.CentreNodeList != null) { //遍历二级目录 for (int j = 0; j < leaderNode.CentreNodeList.Count; j++) { CentreNode centreNode = leaderNode.CentreNodeList[j]; paragraph = doc.CreateParagraph(); //创建段落 //行距 paragraph.setSpacingBetween(30, LineSpacingRule.EXACT); //固定值,30磅 //对齐方式 paragraph.Alignment = ParagraphAlignment.BOTH; //两端对齐 //大纲级别 cT_DecimalNumber = new CT_DecimalNumber(); cT_DecimalNumber.val = "1级";//2级目录 paragraph.GetCTP().AddNewPPr().outlineLvl = cT_DecimalNumber; //标题信息 run = paragraph.CreateRun(); run.SetText((i + 1) + "." + (j + 1)); //2级 run.FontSize = 14; //字体大小 run.FontFamily = "Times New Roman"; //字体 run = paragraph.CreateRun(); run.SetText(" " + centreNode.Name); //2级 run.FontSize = 14; //字体大小 run.FontFamily = "宋体"; //字体 //根据节点信息在数据库中查询 se = (from s in myEntity.Section where s.name == centreNode.Name select s).ToList(); section = se.First();//章节对象 //判断章节中是否存在内容 flag = IsContainTable(centreNode.Name, doc, readDoc); if (flag) { //根据节点对象id信息在数据库内容表中查询 var contents = (from c in myEntity.ContentTab where c.section_id == section.Id select c).ToList(); if (contents.Count > 0) { foreach (ContentTab contentTab in contents) { paragraph = doc.CreateParagraph(); //创建段落 paragraph.IndentationFirstLine = MainForm.Indentation("宋体", 14, 4, new FontStyle()); //首行缩进 XWPFRun r = paragraph.CreateRun(); r.SetText(contentTab.content); r.FontFamily = "宋体"; r.FontSize = 14; } } } if (centreNode.LastNodeList != null) { //遍历三级目录 for (int k = 0; k < centreNode.LastNodeList.Count; k++) { LastNode lastNode = centreNode.LastNodeList[k]; paragraph = doc.CreateParagraph(); //创建段落 //行距 paragraph.setSpacingBetween(30, LineSpacingRule.EXACT); //固定值,30磅 //对齐方式 paragraph.Alignment = ParagraphAlignment.BOTH; //两端对齐 //大纲级别 cT_DecimalNumber = new CT_DecimalNumber(); cT_DecimalNumber.val = "2级";//3级目录 paragraph.GetCTP().AddNewPPr().outlineLvl = cT_DecimalNumber; //标题信息 run = paragraph.CreateRun(); run.SetText((i + 1) + "." + (j + 1) + "." + (k + 1)); //3级 run.FontSize = 14; //字体大小 run.FontFamily = "Times New Roman"; //字体 run = paragraph.CreateRun(); run.SetText(" " + lastNode.Name); //3级 run.FontSize = 14; //字体大小 run.FontFamily = "宋体"; //字体 //根据节点信息在数据库中查询 se = (from s in myEntity.Section where s.name == lastNode.Name select s).ToList(); section = se.First(); //章节对象 //判断章节中是否存在内容 flag = IsContainTable(lastNode.Name, doc, readDoc); if (flag) { //根据节点对象id信息在数据库内容表中查询 var contents = (from c in myEntity.ContentTab where c.section_id == section.Id select c).ToList(); if (contents.Count > 0) { foreach (ContentTab contentTab in contents) { paragraph = doc.CreateParagraph(); //创建段落 paragraph.IndentationFirstLine = MainForm.Indentation("宋体", 14, 4, new FontStyle()); //首行缩进 XWPFRun r = paragraph.CreateRun(); r.SetText(contentTab.content); r.FontFamily = "宋体"; r.FontSize = 14; } } } } } } } } stream.Close(); } }