现在的位置: 首页 > 综合 > 正文

c#操作word文档(转自小排_流浪狗)

2014年02月13日 ⁄ 综合 ⁄ 共 4492字 ⁄ 字号 评论关闭

下图是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

抱歉!评论已关闭.