c#操作word文檔(轉自小排_流浪狗)

下圖是Word對像模型

Application :用來表現WORD應用程序,包含其它所有對象。他的成員經常應用於整個WORD,你可以用它的屬性和方法控制WORD環境。
Document :Document對象是WORD編程的核心。當你打開打開一個已有的文檔或創建一個新的文檔時,就創建了一個新的Documnet對象, 新創建的Document將會被添加到Word Documents Collection。
Selection :Selection對象是描述當前選中的區域。若選擇區域爲空,則認爲是當前光標處。
Rang :是Document的連續部分,根據起始字符和結束字符定義位置。
Bookmark:類似於Rang,但Bookmark可以有名字並在保存Document時Bookmark也被保存。

以下代碼則爲打開一個WORD2003文件。

        public void CreateWordDocument(string FileName)
        {
            if(FileName == "") return;
            this.thisApplication =
                new Microsoft.Office.Interop.Word.ApplicationClass();
            thisApplication.Visible = true;
            thisApplication.Caption = "";
            thisApplication.Options.CheckSpellingAsYouType = false;
            thisApplication.Options.CheckGrammarAsYouType = false;

            Object filename = FileName;
            Object ConfirmConversions = false;
            Object ReadOnly = true;
            Object AddToRecentFiles = false;

            Object PasswordDocument = System.Type.Missing;
            Object PasswordTemplate = System.Type.Missing;
            Object Revert = System.Type.Missing;
            Object WritePasswordDocument = System.Type.Missing;
            Object WritePasswordTemplate = System.Type.Missing;
            Object Format = System.Type.Missing;
            Object Encoding = System.Type.Missing;
            Object Visible = System.Type.Missing;
            Object OpenAndRepair = System.Type.Missing;
            Object DocumentDirection = System.Type.Missing;
            Object NoEncodingDialog = System.Type.Missing;
            Object XMLTransform = System.Type.Missing;

//            Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//                wordApp.Documents.Open(ref filename, ref ConfirmConversions,
//                ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
//                ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
//                ref Encoding, ref Visible);
//            Microsoft.Office.Interop.Word.DocumentClass wordDoc =
//                wordApp.Documents.Open(ref filename, ref ConfirmConversions,
//            ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
//            ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
//            ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog);

            Microsoft.Office.Interop.Word.Document wordDoc =
                thisApplication.Documents.Open(ref filename, ref ConfirmConversions,
                ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate,
                ref Revert,ref WritePasswordDocument, ref WritePasswordTemplate, ref Format,
                ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection,
                ref NoEncodingDialog, ref XMLTransform );

            this.thisDocument = wordDoc;

            formFields = wordDoc.FormFields;
        }

關閉WORD程序程序
Object SaveChanges = false;
Object OriginalFormat = System.Type.Missing;
Object RouteDocument = System.Type.Missing;
this.thisApplication.Quit( ref SaveChanges, ref OriginalFormat, ref RouteDocument );


參考資料:http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/wordobject.asp

上一篇講到,一個Document可能會有多個Rang對象。Rang由起始和結束字符來定他的位置。
以下代碼爲先清空Document裏的內容,再在第一行寫入內容。
    // Clear out any existing information.
    Object start = Type.Missing;
    Object end = Type.Missing;
     Object unit = Type.Missing;
    Object count = Type.Missing;
    ThisDocument.Range(ref start, ref end). Delete(ref unit, ref count);

    // Set up the header information.
    start = 0;
    end = 0;
     rng = ThisDocument.Range(ref start, ref end);
     rng.InsertBefore("Xiaopai");
     rng.Font.Name = "Verdana";
     rng.Font.Size = 16;
     rng.InsertParagraphAfter();//輸入回車

以下爲在剛寫入的內容後添加一個表格。
    object missingValue = Type.Missing;
    object location = 8; //注:若location超過已有字符的長度將會出錯。
    Word.Range rng = ThisDocument.Range(ref location, ref location);
    ThisDocument.Tables.Add(rng, 3, 4, ref missingValue, ref missingValue);

以下爲在剛創建的表格裏添加一行
    Word.Table tbl = ThisDocument.Tables[1]; //第一個表格爲1,而不是0
    Object beforeRow = Type.Missing;
    tbl.Rows.Add(ref beforeRow); //在表格的最後添加一行

填充表格內容
    tbl.Cell(1, 1).Range.Text = "shuai"; //在表格的第一行第一列填入內容。

設置單元格風格
    Word.Range rngCell;
    rngCell = tbl.Cell(1, 2).Range;
    rngCell.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    rngCell.Font.Size = 8;
    rngCell.Font.Name = "Verdana";

當時沒找到合併單元格的方法。有誰知道的共享一下哈。

參考資料:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtocreatewordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wrcore/html/wrtskhowtoaddrowscolumnstowordtables.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_vsto2003_ta/html/odc_VSTWordtbl.asp
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章