2012-06-19 49 views
0

我想使用Open Xml Open Method打開文檔文件,搜索文檔內的書籤,從aspx頁面中的文本框或標籤分配文本到書籤並最終保存到一個新的文檔。在我使用Office Interop Word Method之前。現在我想在Open Xml Method中嘗試同樣的事情。但我無法實現它。你可以幫幫我嗎?提前致謝。打開現有的Word文檔,編輯其書籤並使用Open Xml方法保存爲新文檔

供您參考我的辦公室互操作的代碼是如下,

object Nothing = System.Reflection.Missing.Value; 
    object format = Word.WdSaveFormat.wdFormatDocument; 

    Word.Application wordApp = new Word.ApplicationClass(); 

    object srcFileName = Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Templates\Employment Certificate.doc")); 

    Word.Document wordDoc = wordApp.Documents.Open 
    (ref srcFileName, ref format, ref Nothing, ref Nothing, 
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
    ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, 
    ref Nothing, ref Nothing); 

     object bookmarkDate = "Date"; 
     wordDoc.Bookmarks.get_Item(ref bookmarkDate).Select(); 

     wordApp.Selection.Text = string.Format("{0:MM/dd/yyyy}", lblRequestdate.Text); 

     string DocName; 
     DocName = string.Format("{0}_employment_certificate", lblRequestNo.Text); 
     hFilename.Value = DocName; 
     wordDoc.SaveAs(Server.MapPath(ResolveUrl(@"~/HRLetter\Arabia\Letters\" + DocName + ".doc"))); 


     wordDoc.Close(ref Nothing, ref Nothing, ref Nothing); 
     if (wordDoc != null) 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc); 
      wordDoc = null; 
     } 

     wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); 
     if (wordApp != null) 
     { 
      System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp); 
      wordApp = null; 
     } 
    GC.Collect(); 

回答

0

我能夠使用開放的XML的方法來實現它。

相關問題