知识大全 C#封装Word常用操作类

Posted

篇首语:没有理想,那些所谓的奋斗都是空话。本文由小常识网(cha138.com)小编为大家整理,主要介绍了知识大全 C#封装Word常用操作类相关的知识,希望对你有一定的参考价值。

C#封装Word常用操作类  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!

  using System;    using System Collections Generic;    using System Text;    using Microsoft Office Interop Word;    using System Diagnostics;    namespace OfficeManager            public class WordClass : IDisposable                    #region 字段            private _Application m_WordApp = null;            private _Document m_Document = null;            private object missing = System Reflection Missing Value;            #endregion            #region 构造函数与析构函数            public WordClass()                            m_WordApp = new ApplicationClass();                        ~WordClass()                            try                                    if (m_WordApp != null)                        m_WordApp Quit(ref missing ref missing ref missing);                                catch (Exception ex)                                    Debug Write(ex ToString());                                        #endregion            #region 属性            public _Document Document                            get                                    return m_Document;                                        public _Application WordApplication                            get                                    return m_WordApp;                                        public int WordCount                            get                                    if (m_Document != null)                                            Range rng = m_Document Content;                        rng Select();                        return m_Document Characters Count;                                        else                        return ;                                        public object Missing                            get                                    return missing;                                        #endregion            #region 基本任务            #region CreateDocument            public void CreateDocument(string template)                            object obj_template = template;                if (template Length <= ) obj_template = missing;                m_Document = m_WordApp Documents Add(ref obj_template ref missing ref missing ref missing);                        public void CreateDocument()                            this CreateDocument( );                        #endregion            #region OpenDocument            public void OpenDocument(string fileName bool readOnly)                            object obj_FileName = fileName;                object obj_ReadOnly = readOnly;                m_Document = m_WordApp Documents Open(ref obj_FileName ref missing ref obj_ReadOnly ref missing                     ref missing ref missing ref missing ref missing ref missing ref missing ref missing ref missing                     ref missing ref missing ref missing ref missing);                        public void OpenDocument(string fileName)                            this OpenDocument(fileName false);                        #endregion            #region Save & SaveAs            public void Save()                            if (m_Document != null)                    m_Document Save();                        public void SaveAs(string fileName)                            object obj_FileName = fileName;                if (m_Document != null)                                    m_Document SaveAs(ref obj_FileName ref missing ref missing ref missing ref missing                         ref missing ref missing ref missing ref missing ref missing ref missing ref missing                         ref missing ref missing ref missing ref missing);                                        #endregion            #region Close            public void Close(bool isSaveChanges)                            object saveChanges = WdSaveOptions wdDoNotSaveChanges;                if (isSaveChanges)                    saveChanges = WdSaveOptions wdSaveChanges;                if (m_Document != null)                    m_Document Close(ref saveChanges ref missing ref missing);                        #endregion            #region 添加数据            /// <summary>            /// 添加图片            /// </summary>            /// <param name= picName ></param>            public void AddPicture(string picName)                            if (m_WordApp != null)                    m_WordApp Selection InlineShapes AddPicture(picName ref missing ref missing ref missing);                        /// <summary>            /// 插入页眉            /// </summary>            /// <param name= text ></param>            /// <param name= align ></param>            public void SetHeader(string text WdParagraphAlignment align)                            this m_WordApp ActiveWindow View Type = WdViewType wdOutlineView;                this m_WordApp ActiveWindow View SeekView = WdSeekView wdSeekPrimaryHeader;                this m_WordApp ActiveWindow ActivePane Selection InsertAfter(text); //插入文本                this m_WordApp Selection ParagraphFormat Alignment = align;  //设置对齐方式                this m_WordApp ActiveWindow View SeekView = WdSeekView wdSeekMainDocument; // 跳出页眉设置                        /// <summary>            /// 插入页脚            /// </summary>            /// <param name= text ></param>            /// <param name= align ></param>            public void SetFooter(string text WdParagraphAlignment align)                            this m_WordApp ActiveWindow View Type = WdViewType wdOutlineView;                this m_WordApp ActiveWindow View SeekView = WdSeekView wdSeekPrimaryFooter;                this m_WordApp ActiveWindow ActivePane Selection InsertAfter(text); //插入文本                this m_WordApp Selection ParagraphFormat Alignment = align;  //设置对齐方式                this m_WordApp ActiveWindow View SeekView = WdSeekView wdSeekMainDocument; // 跳出页眉设置                        #endregion            #region Print 

  public void PrintOut()                            object copies = ;                object pages = ;                object range = WdPrintOutRange wdPrintAllDocument;                object items = WdPrintOutItem wdPrintDocumentContent;                object pageType = WdPrintOutPages wdPrintAllPages;                object oTrue = true;                object oFalse = false;                this m_Document PrintOut(                    ref oTrue ref oFalse ref range ref missing ref missing ref missing                     ref items ref copies ref pages ref pageType ref oFalse ref oTrue                     ref missing ref oFalse ref missing ref missing ref missing ref missing);                        public void PrintPreview()                            if (m_Document != null)                    m_Document PrintPreview();                        #endregion            public void Paste()                            try                                    if (m_Document != null)                                            m_Document ActiveWindow Selection Paste();                                                    catch (Exception ex)                                    Debug Write(ex Message);                                        #endregion            #region 文档中的文本和对象            public void AppendText(string text)                            Selection currentSelection = this m_WordApp Selection;                // Store the user s current Overtype selection                bool userOvertype = this m_WordApp Options Overtype;                // Make sure Overtype is turned off                 if (this m_WordApp Options Overtype)                                    this m_WordApp Options Overtype = false;                                // Test to see if selection is an insertion point                 if (currentSelection Type == WdSelectionType wdSelectionIP)                                    currentSelection TypeText(text);                    currentSelection TypeParagraph();                                else                    if (currentSelection Type == WdSelectionType wdSelectionNormal)                                            // Move to start of selection                         if (this m_WordApp Options ReplaceSelection)                                                    object direction = WdCollapseDirection wdCollapseStart;                            currentSelection Collapse(ref direction);                                                currentSelection TypeText(text);                        currentSelection TypeParagraph();                                        else                                            // Do nothing                                     // Restore the user s Overtype selection                this m_WordApp Options Overtype = userOvertype;                        #endregion            #region 搜索和替换文档中的文本            public void Replace(string oriText string replaceText)                            object replaceAll = WdReplace wdReplaceAll;                this m_WordApp Selection Find ClearFormatting();                this m_WordApp Selection Find Text = oriText;                this m_WordApp Selection Find Replacement ClearFormatting();                this m_WordApp Selection Find Replacement Text = replaceText;                this m_WordApp Selection Find Execute(                    ref missing ref missing ref missing ref missing ref missing                     ref missing ref missing ref missing ref missing ref missing                     ref replaceAll ref missing ref missing ref missing ref missing);           

cha138/Article/program/ASP/201311/21787

相关参考

知识大全 c# word操作

  word操作包括打开word文档创建表格合并单元格保存退出  WordOperator  usingSystem;   usingSystemWeb; &n

知识大全 C#操作Word文档(Office 2007)

C#操作Word文档(Office2007)  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!&nbs

知识大全 Java操作文本封装类

Java操作文本封装类  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  importjavaioB

知识大全 C#中对DatagridView部分常用操作

C#中对DatagridView部分常用操作  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  (最

知识大全 C#中对DatagridView的部分常用操作

C#中对DatagridView的部分常用操作  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  (

知识大全 通过jacob对excel,word进行打印等操作

  因为项目有个需求要求能够在某个预定时间对特定报表进行自动打印报表的形式主要是excel如果是c++或者c#什么的简直是小菜一碟可惜偏偏是java实现并且这又不同于常见的页面手工打印可以通过java

知识大全 c# Stack操作类

c#Stack操作类  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  Stack类代码  usin

知识大全 C# FTP操作类

C#FTP操作类  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  最近要做个用ftp读取文件并在浏

知识大全 C#打开WORD文档内容并显示

C#打开WORD文档内容并显示  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  C#打开WORD文

知识大全 C#编程实现动态生成Word文档

C#编程实现动态生成Word文档  以下文字资料是由(全榜网网www.cha138.com)小编为大家搜集整理后发布的内容,让我们赶快一起来看一下吧!  如何用C#编程实