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

c# word操作

2022-06-13   來源: .NET編程 

  word操作包括打開word文檔創建表格合並單元格保存退出

  WordOperator

  using System;
    using SystemWeb;
    using MicrosoftOfficeInteropWord;

  namespace CustomFileAccess
    {
        public class WordOperator
        {
            public void CreateWord()
            {
                MicrosoftOfficeInteropWordApplication wordApp = new Application();
                MicrosoftOfficeInteropWordDocument wordDocument = new Document();
                MicrosoftOfficeInteropWordTable wordTable;
                MicrosoftOfficeInteropWordTable wordTableCopy;

  object myNull = SystemReflectionMissingValue;

  object strPath = HttpContextCurrentServerMapPath(@WordTemplete\MyWordTempletedoc);
                object styleName = Table Grid ;

  try
                {
                    wordDocument = wordAppDocumentsOpen(ref strPath ref myNull ref myNull ref myNull
                        ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull ref myNull
                        ref myNull ref myNull ref myNull ref myNull);

  object start = ;
                    object end = ;

  MicrosoftOfficeInteropWordRange wordRange = wordDocumentRange(ref start ref end);

  wordTable = wordDocumentTablesAdd(wordRange ref myNull ref myNull);

  wordTableBordersOutsideColor = WdColorwdColorAutomatic;
                    wordTableBordersOutsideColorIndex = WdColorIndexwdAuto;
                    wordTableBordersOutsideLineStyle = WdLineStylewdLineStyleSingle;
                    wordTableBordersOutsideLineWidth = WdLineWidthwdLineWidthpt;

  wordTableBordersInsideColor = WdColorwdColorAutomatic;
                    wordTableBordersInsideColorIndex = WdColorIndexwdAuto;
                    wordTableBordersInsideLineStyle = WdLineStylewdLineStyleSingle;
                    wordTableBordersInsideLineWidth = WdLineWidthwdLineWidthpt;

  wordDocumentTables[]Cell( )Merge(wordDocumentTables[]Cell( ));

  wordDocumentTables[]Cell( )RangeText = cell ;
                    wordDocumentTables[]Cell( )RangeText = cell ;

  wordDocumentTables[]Select();
                    wordAppSelectionCopy();

  wordDocumentTables[]Cell( )RangeText = The First Table;

  object myunit = MicrosoftOfficeInteropWordWdUnitswdStory;
                    wordAppSelectionEndKey(ref myunit ref myNull);

  wordAppSelectionTypeParagraph();

  wordAppSelectionPaste();

  wordDocumentTables[]Cell( )RangeText = The Second Table;

  wordDocumentSave();
                }
                catch
                {
                    wordDocumentClose(ref myNull ref myNull ref myNull);
                    wordAppQuit(ref myNull ref myNull ref myNull);
                    if (wordDocument != null)
                    {
                        SystemRuntimeInteropServicesMarshalReleaseComObject(wordDocument);
                        wordDocument = null;
                    }
                    if (wordApp != null)
                    {
                        SystemRuntimeInteropServicesMarshalReleaseComObject(wordApp);
                        wordApp = null;
                    }
                    GCCollect();
                    throw new Exception(文檔生成失敗!);

  }
                finally
                {
                    wordDocumentClose(ref myNull ref myNull ref myNull);
                    wordAppQuit(ref myNull ref myNull ref myNull);
                }

  }
        }
    }


From:http://tw.wingwit.com/Article/program/net/201311/13344.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.