使用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);
            }
        }

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