Word操作

    技术2022-05-19  28

    首先添加引用,解决方案资源管理器-》引用-》添加-》Com-》浏览-》C:/Program Files/Microsoft Office/OFFICE11/MSWORD.OLB    我使用的是office 2003其他版本我不太清楚,.net会自动把OLB控件转换成DLL文件

    使用方法:

      object oMissing = System.Reflection.Missing.Value;

       Word.Application oWord =new Word.Application();

      oWord.Visible = false;//设置Word应用程序为不可见

    //新建一个Word文档   Word.Document oDoc=oWord.Documents.Add(ref oMissing,ref oMissing ,ref oMissing,ref oMissing);   

    //文档内容的复制与粘贴

        oDoc.Content.Copy();    oDoc.Content.Paste()

    //文档的另存为

    oDoc.SaveAs(ref fileName,ref saveFormat,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing,ref oMissing);

    //其他设置

       oDoc.PageSetup.PaperSize=Word.WdPaperSize.wdPaperA3;//页面设置   oDoc.PageSetup.Orientation=Word.WdOrientation.wdOrientLandscape;//横板还是竖板   oDoc.PageSetup.TextColumns.SetCount(2);//分栏

    //关闭Word

       oWord.Application.Quit(ref b,ref oMissing,ref oMissing);   System.Runtime.InteropServices.Marshal.ReleaseComObject(oWord);

    通过oDoc对象对Word文档进行操作(word能做的它都能做)进行操作里面有很多函数,有兴趣的自己研究


    最新回复(0)