熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> ASP編程 >> 正文

C#封裝Word常用操作類

2022-06-13   來源: ASP編程 

  using System;
    using SystemCollectionsGeneric;
    using SystemText;
    using MicrosoftOfficeInteropWord;
    using SystemDiagnostics;
    namespace OfficeManager
    {
        public class WordClass : IDisposable
        {
            #region 字段
            private _Application m_WordApp = null;
            private _Document m_Document = null;
            private object missing = SystemReflectionMissingValue;
            #endregion
            #region 構造函數與析構函數
            public WordClass()
            {
                m_WordApp = new ApplicationClass();
            }
            ~WordClass()
            {
                try
                {
                    if (m_WordApp != null)
                        m_WordAppQuit(ref missing ref missing ref missing);
                }
                catch (Exception ex)
                {
                    DebugWrite(exToString());
                }
            }
            #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_DocumentContent;
                        rngSelect();
                        return m_DocumentCharactersCount;
                    }
                    else
                        return ;
                }
            }
            public object Missing
            {
                get
                {
                    return missing;
                }
            }
            #endregion
            #region 基本任務
            #region CreateDocument
            public void CreateDocument(string template)
            {
                object obj_template = template;
                if (templateLength <= ) obj_template = missing;
                m_Document = m_WordAppDocumentsAdd(ref obj_template ref missing ref missing ref missing);
            }
            public void CreateDocument()
            {
                thisCreateDocument();
            }
            #endregion
            #region OpenDocument
            public void OpenDocument(string fileName bool readOnly)
            {
                object obj_FileName = fileName;
                object obj_ReadOnly = readOnly;
                m_Document = m_WordAppDocumentsOpen(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)
            {
                thisOpenDocument(fileName false);
            }
            #endregion
            #region Save & SaveAs
            public void Save()
            {
                if (m_Document != null)
                    m_DocumentSave();
            }
            public void SaveAs(string fileName)
            {
                object obj_FileName = fileName;
                if (m_Document != null)
                {
                    m_DocumentSaveAs(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 = WdSaveOptionswdDoNotSaveChanges;
                if (isSaveChanges)
                    saveChanges = WdSaveOptionswdSaveChanges;
                if (m_Document != null)
                    m_DocumentClose(ref saveChanges ref missing ref missing);
            }
            #endregion
            #region 添加數據
            /// <summary>
            /// 添加圖片
            /// </summary>
            /// <param name=picName></param>
            public void AddPicture(string picName)
            {
                if (m_WordApp != null)
                    m_WordAppSelectionInlineShapesAddPicture(picName ref missing ref missing ref missing);
            }
            /// <summary>
            /// 插入頁眉
            /// </summary>
            /// <param name=text></param>
            /// <param name=align></param>
            public void SetHeader(string text WdParagraphAlignment align)
            {
                thism_WordAppActiveWindowViewType = WdViewTypewdOutlineView;
                thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekPrimaryHeader;
                thism_WordAppActiveWindowActivePaneSelectionInsertAfter(text); //插入文本
                thism_WordAppSelectionParagraphFormatAlignment = align;  //設置對齊方式
                thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekMainDocument; // 跳出頁眉設置
            }
            /// <summary>
            /// 插入頁腳
            /// </summary>
            /// <param name=text></param>
            /// <param name=align></param>
            public void SetFooter(string text WdParagraphAlignment align)
            {
                thism_WordAppActiveWindowViewType = WdViewTypewdOutlineView;
                thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekPrimaryFooter;
                thism_WordAppActiveWindowActivePaneSelectionInsertAfter(text); //插入文本
                thism_WordAppSelectionParagraphFormatAlignment = align;  //設置對齊方式
                thism_WordAppActiveWindowViewSeekView = WdSeekViewwdSeekMainDocument; // 跳出頁眉設置
            }
            #endregion
            #region Print
 

  public void PrintOut()
            {
                object copies = ;
                object pages = ;
                object range = WdPrintOutRangewdPrintAllDocument;
                object items = WdPrintOutItemwdPrintDocumentContent;
                object pageType = WdPrintOutPageswdPrintAllPages;
                object oTrue = true;
                object oFalse = false;
                thism_DocumentPrintOut(
                    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_DocumentPrintPreview();
            }
            #endregion
            public void Paste()
            {
                try
                {
                    if (m_Document != null)
                    {
                        m_DocumentActiveWindowSelectionPaste();
                    }
                }
                catch (Exception ex)
                {
                    DebugWrite(exMessage);
                }
            }
            #endregion
            #region 文檔中的文本和對象
            public void AppendText(string text)
            {
                Selection currentSelection = thism_WordAppSelection;
                // Store the users current Overtype selection
                bool userOvertype = thism_WordAppOptionsOvertype;
                // Make sure Overtype is turned off
                if (thism_WordAppOptionsOvertype)
                {
                    thism_WordAppOptionsOvertype = false;
                }
                // Test to see if selection is an insertion point
                if (currentSelectionType == WdSelectionTypewdSelectionIP)
                {
                    currentSelectionTypeText(text);
                    currentSelectionTypeParagraph();
                }
                else
                    if (currentSelectionType == WdSelectionTypewdSelectionNormal)
                    {
                        // Move to start of selection
                        if (thism_WordAppOptionsReplaceSelection)
                        {
                            object direction = WdCollapseDirectionwdCollapseStart;
                            currentSelectionCollapse(ref direction);
                        }
                        currentSelectionTypeText(text);
                        currentSelectionTypeParagraph();
                    }
                    else
                    {
                        // Do nothing
                    }
                // Restore the users Overtype selection
                thism_WordAppOptionsOvertype = userOvertype;
            }
            #endregion
            #region 搜索和替換文檔中的文本
            public void Replace(string oriText string replaceText)
            {
                object replaceAll = WdReplacewdReplaceAll;
                thism_WordAppSelectionFindClearFormatting();
                thism_WordAppSelectionFindText = oriText;
                thism_WordAppSelectionFindReplacementClearFormatting();
                thism_WordAppSelectionFindReplacementText = replaceText;
                thism_WordAppSelectionFindExecute(
                    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);
            }

  #endregion
            #region 文檔中的Word表格
            /// <summary>
            /// 向文檔中插入表格
            /// </summary>
            /// <param name=startIndex>開始位置</param>
            /// <param name=endIndex>結束位置</param>
            /// <param name=rowCount>行數</param>
            /// <param name=columnCount>列數</param>
            /// <returns></returns>
            public Table AppendTable(int startIndex int endIndex int rowCount int columnCount)
            {
                object start = startIndex;
                object end = endIndex;
                Range tableLocation = thism_DocumentRange(ref start ref end);
                return thism_DocumentTablesAdd(tableLocation rowCount columnCount ref missing ref missing);
            }
            /// <summary>
            /// 添加行
            /// </summary>
            /// <param name=table></param>
            /// <returns></returns>
            public Row AppendRow(Table table)
            {
                object row = tableRows[];
                return tableRowsAdd(ref row);
            }
            /// <summary>
            /// 添加列
            /// </summary>
            /// <param name=table></param>
            /// <returns></returns>
            public Column AppendColumn(Table table)
            {
                object column = tableColumns[];
                return tableColumnsAdd(ref column);
            }
            /// <summary>
            /// 設置單元格的文本和對齊方式
            /// </summary>
            /// <param name=cell>單元格</param>
            /// <param name=text>文本</param>
            /// <param name=align>對齊方式</param>
            public void SetCellText(Cell cell string text WdParagraphAlignment align)
            {
                cellRangeText = text;
                cellRangeParagraphFormatAlignment = align;
            }
            #endregion
            #region IDisposable 成員
            public void Dispose()
            {
                try
                {
                    if (m_WordApp != null)
                        m_WordAppQuit(ref missing ref missing ref missing);
                }
                catch (Exception ex)
                {
                    DebugWrite(exToString());
                }
            }
            #endregion
        }
    }


From:http://tw.wingwit.com/Article/program/ASP/201311/21787.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.