使用C#进行WORD操作(二)

上篇写到了如何新建并保存一个word文档,现在写一下如何对文档插入一些内容,我们将这写操作保存到了一个类里
class WordHandle
    {
        private object path;//文件路径
        private MSWord.Application wordApp;//Word应用程序变量
        private MSWord.Document wordDoc;//Word文档变量

        public WordHandle()
        {
            try
            {
                path = @"d:\母版1.docx";
                object Nothing = Missing.Value;
                object oReadOnly = true;
                wordApp = new MSWord.ApplicationClass();
                wordDoc = wordApp.Documents.Open(ref path, ref Nothing, ref oReadOnly, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }

        public void GenerateAndSave(ElectionMode electionMode)//从electionMode中获取相关数据
        {
            UpdateBookMark(electionMode);
            UpdateTable(electionMode);
            UpdateShape(electionMode);
            GetAllBookmarks();
            object Nothing = Missing.Value;//关闭文档
            object filename = "d:\\ballot";

            object format = MSWord.WdSaveFormat.wdFormatDocument;//保存格式 
            wordDoc.SaveAs(ref filename, 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);
            //关闭wordDoc,wordApp对象              
            wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
            wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
        }

        private void UpdateBookMark(ElectionMode electionMode)//修改书签对应的文本
        {    //此为一种获取书签的方式
            Microsoft.Office.Interop.Word.Bookmark ballotTitle = wordDoc.Bookmarks[3];
            Microsoft.Office.Interop.Word.Bookmark ballotRule = wordDoc.Bookmarks[2];
            //书签.Range.text属性即为书签对应的文本
            var ballotName = electionMode.BusinessMode_BallotArray[0].Name;
            ballotTitle.Range.Text = ballotName;
            string message = "fuck!!!";
            ballotRule.Range.Text = message;
        }

        private void UpdateTable(ElectionMode electionMode)
        {
            Microsoft.Office.Interop.Word.Table table = wordDoc.Tables[1];

            var ballot = electionMode.BusinessMode_BallotArray[0];
            int countColumn = table.Columns.Count + 1;
            object Nothing = Missing.Value;

            for (int i = 0; i < ballot.BusinessMode_SubkindArray.Length; i++)
            {
                var subkindName = ballot.BusinessMode_SubkindArray[i].Name;
                var subkind = ballot.BusinessMode_SubkindArray[i];

                for (int j = 0; j < subkind.BusinessMode_CandidateArray.Length; j++)
                {
                    var candidateName = subkind.BusinessMode_CandidateArray[j].Name;
                    //添加新的一列,并填充
                    table.Columns.Add(ref Nothing);
                    table.Columns[countColumn].Width = 34;
                    table.Cell(2, countColumn).Range.Text = candidateName;
                    //table.Cell(2, countColumn).Range.t
                    countColumn++;
                }
                var otherNum = subkind.BusinessMode_OtherArray.Length;
                //添加对应数量的空列并合并第一行
                for (int k = 0; k < otherNum; k++)
                {
                    table.Columns.Add(ref Nothing);
                    countColumn++;
                }
            }
            //删除某一列
            table.Columns[2].Delete();
            //合并职务这一列
            for (int i = 0; i < ballot.BusinessMode_SubkindArray.Length; i++)
            {
                var subkindName = ballot.BusinessMode_SubkindArray[i].Name;
                var subkind = ballot.BusinessMode_SubkindArray[i];
                int sumCand = subkind.BusinessMode_CandidateArray.Length + subkind.BusinessMode_OtherArray.Length;
                table.Cell(1, i + 2).Merge(table.Cell(1, i + sumCand + 1));
                table.Cell(1, i + 2).Range.Text = subkindName;
            }
        }

        private void UpdateShape(ElectionMode electionMode)
        {
            var ballotId = electionMode.BusinessMode_BallotArray[0].BallotID;
            string topPath = "初始化";
            string bottomPath = "初始化";
            object linkToFile = true;
            object saveWithDocument = true;
            object Nothing = Missing.Value;

            object bookMark_1 = "topshape";

            //查找书签,此为另一种获取书签的方式
            wordDoc.Bookmarks.get_Item(ref bookMark_1).Select();
            //设置图片位置
            wordApp.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
            //在书签的位置添加图片
            MSWord.InlineShape inlineShape = wordApp.Selection.InlineShapes.AddPicture(topPath, ref linkToFile, ref saveWithDocument, ref Nothing);
            
        }

        private void GetAllBookmarks()
        {
            for(int markPos=1; markPos<=wordDoc.Bookmarks.Count; markPos++)//获取文档中所有的书签的名字,对应的文本
            {
                Console.WriteLine("mark "+ markPos + " name is:" + wordDoc.Bookmarks[markPos].Name + ";text is:"+ wordDoc.Bookmarks[markPos].Range.Text);
            }
        }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章