GenerateWord Enterprise Architect scenario and TestCase to docment

using EA;
using EA_AutoModeler_64.Activity_Analysis_Class;
using Microsoft.Office.Interop.Word;
using Org.BouncyCastle.Utilities.Collections;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Windows;
using System.Windows.Controls;
using static System.Net.Mime.MediaTypeNames;

namespace EA_AutoModeler_64
{
    class AcivityAnalysis
    {
        public static void GenerateWord(EA.Repository repository, EA.Diagram diagram)
        {
            #region 导出word

            WordDocument wordDocument = new WordDocument();
            wordDocument.repository = repository;
            //wordDocument.path = path;

            //建立word文档
            wordDocument.wordApp = new Microsoft.Office.Interop.Word.Application();
            wordDocument.wordApp.Visible = true;
            object oTemplate = "";
            object Nothing = System.Type.Missing;
            //以模板为基础生成文档
            wordDocument.wordDoc = wordDocument.wordApp.Documents.Add(ref oTemplate, ref Nothing, ref Nothing, ref Nothing);
            Microsoft.Office.Interop.Word.Range range = wordDocument.wordDoc.Range();
            range.Text = "";
            //把文档生成器初始化
            //wordDocument.DocumentorInstance();
            //目录书签
            string bookmark = "Catalog";
            wordDocument.wordDoc.Bookmarks.Add(bookmark, wordDocument.wordDoc.Paragraphs.Last.Range);

            //Log logForm = new Log();
            //logForm.Show();
            try
            {

                Model.repository = repository;
                wordDocument.GenerateTitle(diagram.Name, 1);//图标题
                Model.GenerateDiagram(diagram, wordDocument.wordApp, wordDocument.wordDoc);//生成图
                if (diagram.Type == "Activity")
                {
                    UseCaseAnalysis.Interface(diagram, repository, wordDocument, 1);
                }
            }
            catch (Exception w)
            {
                MessageBox.Show(w.Message);
                wordDocument.wordApp.Quit();
                return;
            }
            //替换关键词,删除空行
            wordDocument.ReplaceKeywords(wordDocument.wordApp, wordDocument.wordDoc);

            #region 关闭文档
            //object format = WdSaveFormat.wdFormatDocumentDefault;
            //object pathObject = (object)path;
            //wordDocument.wordDoc.SaveAs(ref pathObject, ref format, ref Nothing, ref Nothing, ref Nothing,
            //    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
            //    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            Clipboard.Clear();

            #endregion

            #endregion
        }


        public static void GenerateWord(EA.Repository repository, EA.Diagram curremtDiagram, EA.Element eleObj, object oTemplate = null, string clearText = "")
        {
            #region 导出word

            WordDocument wordDocument = new WordDocument();
            wordDocument.repository = repository;
            //wordDocument.path = path;

            //建立word文档
            wordDocument.wordApp = new Microsoft.Office.Interop.Word.Application();
            wordDocument.wordApp.Visible = true;
            //object oTemplate = "";
            object Nothing = System.Type.Missing;
            //以模板为基础生成文档
            wordDocument.wordDoc = wordDocument.wordApp.Documents.Add(ref oTemplate, ref Nothing, ref Nothing, ref Nothing);
            Microsoft.Office.Interop.Word.Range range = wordDocument.wordDoc.Range();
            if (string.IsNullOrEmpty(clearText))
            {
                range.Text = "";
            }            
            //把文档生成器初始化
            //wordDocument.DocumentorInstance();
            //目录书签
            string bookmark = "Catalog";
            wordDocument.wordDoc.Bookmarks.Add(bookmark, wordDocument.wordDoc.Paragraphs.Last.Range);

            //Log logForm = new Log();
            //logForm.Show();
            try
            {
                //Microsoft.Office.Interop.Word.Range rangeToInsert = wordDocument.wordDoc.Content;
                string insertAfterText = "测试用例的跟踪关系";
                //rangeToInsert.Find.Execute(insertAfterText);
                //string contentToInsert = "\n";
                //rangeToInsert.InsertAfter(contentToInsert);
                //wordDocument.wordDoc.Content.InsertAfter("\n");
                Model.repository = repository;
                //wordDocument.GenerateTitle(diagram.Name, 1);//图标题
                Model.GenerateDiagram(curremtDiagram, wordDocument.wordApp, wordDocument.wordDoc, insertAfterText);//生成图
                if (curremtDiagram.Type == "Activity")
                {
                    UseCaseAnalysis.Interface(curremtDiagram, repository, wordDocument, 1);
                }
                // 提取内容生成到文档的表格中
                int key = 1;
                foreach (EA.DiagramObject dObj in curremtDiagram.DiagramObjects)
                {
                    Console.WriteLine(dObj.ElementID);
                    if (key>1)
                    {
                        wordDocument.wordDoc.Content.InsertAfter("\n");
                        var sourceTable = wordDocument.wordDoc.Tables[1];  // 获取要复制的表格
                        sourceTable.Select();
                        // 复制表格
                        wordDocument.wordApp.Selection.Copy();
                        // 取消选中
                        //wordDocument.wordApp.Selection.Collapse();
                        //wordDocument.wordDoc.Content.InsertAfter("\n");
                        // 粘贴表格
                        //wordDocument.wordApp.Selection.Paste(); 
                        // 取消选中
                        //wordDocument.wordApp.Selection.Collapse(); 
                        // 将选择位置移动到新插入的段落后面
                        //移动光标文档末尾
                        object count = wordDocument.wordDoc.Paragraphs.Count;
                        object WdLine = Microsoft.Office.Interop.Word.WdUnits.wdParagraph;
                        Object oMissing = System.Reflection.Missing.Value;
                        wordDocument.wordApp.Selection.MoveDown(ref WdLine, ref count, ref oMissing);
                        // 在文档末尾粘贴表格
                        wordDocument.wordApp.Selection.PasteAndFormat(WdRecoveryType.wdPasteDefault);
                        // 等待粘贴完成,然后再重新赋值
                    }
                    EA.Element ele = repository.GetElementByID(dObj.ElementID);
                    if (ele.Stereotype.ToLower() == "TestCase".ToLower())
                    {
                        wordDocument.wordDoc.Tables[key].Cell(1, 2).Range.Text = ele.Name;
                        wordDocument.wordDoc.Tables[key].Cell(5, 2).Range.Text = ele.Name;
                        wordDocument.wordDoc.Tables[key].Cell(9, 2).Range.Text = ele.Status;
                        wordDocument.wordDoc.Tables[key].Cell(10, 2).Range.Text = ele.Author;

                        // 获取当前时间转为格式20231009113127
                        DateTime dt = DateTime.Now;
                        wordDocument.wordDoc.Tables[key].Cell(2, 2).Range.Text = dt.ToString("yyyyMMddHHmmss");
                        // 提取操作步骤
                        string caozuo = "";
                        int i = 0;
                        int i9 = 9;
                        int i1 = 7 + i;
                        foreach (EA.Test test in ele.Tests)
                        {
                            Console.WriteLine(test.Name);
                            string decodedNotes = HttpUtility.HtmlDecode(test.Notes);
                            string decodedNotes1 = decodedNotes;
                            i1 = 7 + i;
                            if (i1 >= i9)
                            {
                                //Range MyRange = wordDocument.wordDoc.Paragraphs.Last.Range;
                                //wordDocument.wordDoc.Tables.Add(MyRange, i1, 1);
                                Table table = wordDocument.wordDoc.Tables[key];
                                table.Cell(i1 - 1, 1).Select();//光标选定第二行的第一个单元格
                                wordDocument.wordApp.Selection.InsertRowsBelow(1);  //在下方插入一行  
                            }
                            wordDocument.wordDoc.Tables[key].Cell(i1, 1).Range.Text = test.Name;
                            string[] lines = decodedNotes.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            if (decodedNotes.Contains("用户"))
                            {
                                caozuo = lines[0];
                            }
                            if (lines.Length > 1)
                            {
                                decodedNotes1 = string.Join(Environment.NewLine, lines.Skip(1).Take(lines.Length).ToArray());
                            }
                            wordDocument.wordDoc.Tables[key].Cell(i1, 2).Range.Text = decodedNotes1;
                            wordDocument.wordDoc.Tables[key].Cell(i1, 3).Range.Text = test.TestResults;
                            i++;
                        }
                        foreach (EA.Scenario scenario in ele.Scenarios)
                        {
                            Console.WriteLine(scenario.Name);
                            string decodedNotes = HttpUtility.HtmlDecode(scenario.Notes);
                            string decodedNotes1 = decodedNotes;
                            i1 = 7 + i;
                            if (i1 >= i9)
                            {
                                //wordDocument.wordDoc.Tables.Add(wordDocument.wordDoc.Paragraphs.Last.Range, i1, 1);
                                Table table = wordDocument.wordDoc.Tables[key];
                                table.Cell(i1-1, 1).Select();//光标选定第二行的第一个单元格
                                wordDocument.wordApp.Selection.InsertRowsBelow(1);  //在下方插入一行                                                                                                            
                            }
                            wordDocument.wordDoc.Tables[key].Cell(i1, 1).Range.Text = scenario.Name;
                            //string[] lines = decodedNotes.Split(new[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
                            //if (decodedNotes.Contains("用户"))
                            //{
                            //    caozuo = lines[0];
                            //}
                            //if (lines.Length > 1)
                            //{
                            //    decodedNotes1 = string.Join(Environment.NewLine, lines.Skip(1).Take(lines.Length).ToArray());
                            //}
                            List<string> StepsList = new List<string>();
                            for(int j = 0; j < scenario.Steps.Count; j++)
                            {
                                StepsList.Add(scenario.Steps.GetAt((short)j).Name.ToString());
                            }
                            wordDocument.wordDoc.Tables[key].Cell(i1, 2).Range.Text = string.Join(Environment.NewLine, StepsList.ToArray());
                            wordDocument.wordDoc.Tables[key].Cell(i1, 3).Range.Text = "";
                            i++;
                        }
                        wordDocument.wordDoc.Tables[key].Cell(4, 2).Range.Text = caozuo;
                        //element.Constraints 循环
                        string constraintText = "";
                        foreach (EA.Constraint constraint in ele.Constraints)
                        {
                            Console.WriteLine(constraint.Name);
                            Console.WriteLine(constraint.Notes);
                            string decodedNotes = HttpUtility.HtmlDecode(constraint.Notes);
                            constraintText += constraint.Name + ":" + decodedNotes + "\n";
                        }
                        wordDocument.wordDoc.Tables[key].Cell(3, 2).Range.Text = constraintText;
                        key++;
                    }
                }
                
                

                //wordDocument.wordDoc.Tables.Add(wordDocument.wordDoc.Paragraphs.Last.Range, 1, 1);
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.Text = ele.Name;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.Font.Bold = 2;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.Font.Size = 14;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.SpaceAfter = 10;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.SpaceBefore = 10;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.LineSpacingRule = Microsoft.Office.Interop.Word.WdLineSpacing.wdLineSpaceSingle;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.LeftIndent = 0;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.RightIndent = 0;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.FirstLineIndent = 0;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.SpaceAfter = 0;
                //wordDocument.wordDoc.Tables[1].Cell(1, 2).Range.ParagraphFormat.SpaceBefore = 0;

            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                //wordDocument.wordApp.Quit();
                //return;
            }
            //替换关键词,删除空行
            wordDocument.ReplaceKeywords(wordDocument.wordApp, wordDocument.wordDoc);

            #region 关闭文档
            //object format = WdSaveFormat.wdFormatDocumentDefault;
            //object pathObject = (object)path;
            //wordDocument.wordDoc.SaveAs(ref pathObject, ref format, ref Nothing, ref Nothing, ref Nothing,
            //    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing,
            //    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            Clipboard.Clear();

            #endregion

            #endregion
        }

    }
}

你可能感兴趣的:(c#,开发语言)