2013-07-02 51 views
1

我想使用退出的Word文檔模板創建一個Word文檔。 所以我嘗試這樣使用C#和asp.net更改Word文檔模板頁腳和標題

private void CreateWordDocument(object fileName, 
           object saveAs) 
{ 
    object missing = System.Reflection.Missing.Value;   
    var wordApp = new Application();   
    Microsoft.Office.Interop.Word.Document aDoc = null; 

    if (File.Exists((string)fileName)) 
    { 
     DateTime today = DateTime.Now; 

     object readOnly = false; 
     object isVisible = false; 

     //Set Word to be not visible. 
     wordApp.Visible = false; 

     //Open the word document 
     aDoc = wordApp.Documents.Open(ref fileName, ref missing, 
      ref readOnly, ref missing, ref missing, ref missing, 
      ref missing, ref missing, ref missing, ref missing, 
      ref missing, ref isVisible, ref missing, ref missing, 
      ref missing, ref missing); 

     aDoc.Activate(); 

     this.FindAndReplace(wordApp, "<Name>", txtName.Text); 
     this.FindAndReplace(wordApp, "<company>", txtcompanyName.Text); 
    } 
    else 
    { 
     return; 
    } 

    //Save the document as the correct file name. 
    aDoc.SaveAs(ref saveAs, 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); 

    //Close the document - you have to do this. 
    aDoc.Close(ref missing, ref missing, ref missing); 
} 

在我的模板頁腳和頁眉部分有一些變量。 但我不知道如何使用變量替換它..

我的標題是這樣的。 enter image description here

怎麼辦?

+0

從ASP.NET或其他服務器技術中使用Office Interop是一個可怕的想法。這些API被編寫用於桌面應用程序,用於自動化Office(一套桌面應用程序)。服務器應用程序在許多方面有所不同,因此在其中使用Office Interop是非常非常糟糕的主意。它也不受Microsoft的支持,並可能違反您的Office許可證。請參見[服務器端自動化Office的注意事項](http://support.microsoft.com/kb/257757) –

回答

0

即使我有相同的要求編輯現有的單詞模板,包括頁眉和頁腳。這是我使用的解決方案,工作得很好。

要在Word模板替換詞:

string destFile = Server.MapPath("~/Letters/sample.docx"); 
    object fileName = destFile; 
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = false }; 
    Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: false); 
    object Unknown = Type.Missing; 
    aDoc.Activate(); 
    try 
    { 
    //Replace any text inside the word document 
    FindAndReplace(wordApp, "[Date]", DateTime.Now.ToString("MMMM dd, yyyy")); 
    } 

要在Word模板的頁腳替換詞:

object replaceAll = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll; 
    foreach (Microsoft.Office.Interop.Word.Section section in aDoc.Sections) 
       { 
        Microsoft.Office.Interop.Word.HeadersFooters footers = section.Footers; 
        foreach (Microsoft.Office.Interop.Word.HeaderFooter footer in footers) 
        { 
         Range footerRange = footer.Range; 
         footerRange.Find.ClearFormatting(); 
         footerRange.Find.Replacement.ClearFormatting(); 
         footerRange.Find.Text = "[Vendor Specialist Phone]"; 
         footerRange.Find.Replacement.Text = ds.Tables[1].Rows[0]["phoneNumber"].ToString(); 
         footerRange.Find.Wrap = WdFindWrap.wdFindContinue; 
         footerRange.Find.Execute(ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref replaceAll, ref Unknown, ref Unknown, ref Unknown, ref Unknown); 
        } 
       } 
      aDoc.SaveAs(ref fileName, 
      ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, 
      ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown, 
      ref Unknown, ref Unknown, ref Unknown, ref Unknown, ref Unknown); 

      } 
      catch (Exception ex) 
      { 
      } 
      finally 
      { 
       //Close the document 
       aDoc.Close(WdSaveOptions.wdSaveChanges); 
       //Close the instance of WINWORD.EXE 
       ((_Application)wordApp).Quit(ref Unknown, ref Unknown, ref Unknown); 
      } 

要更換頭,它作爲頁腳相同。只需使用section.Headers而不是section.Footers。休息一切都是一樣的。

這工作正常沒有任何問題。

謝謝

相關問題