2012-09-23 14 views
0

我有一個gridview,我需要將gridview導出到MS word,這工作正常,但我需要將自定義頁眉和頁腳放在Ms字的每個頁面上。是否可以在導出的gridview上從asp.net運行宏,以便我可以將自定義頁眉和頁腳放在導出的gridview上,並調整打印頁面上的某些頁邊距。從asp.net運行一個宏,同時導出gridview

任何幫助將不勝感激。

謝謝。

回答

1

This tutorial將向您展示如何使用.NET調用宏。這是爲您的方案的相關步驟的總結(您可能有一些這方面的設置已經如果你已成功地出口已經在你的GridView):

添加引用到Microsoft Word中圖書館:

  1. 在項目菜單上,單擊添加引用。
  2. 在COM選項卡,找到Microsoft Word 10.0 Object LibraryMicrosoft Word 11.0 Object Library
  3. 點擊Select

然後將這些引用添加到您的代碼背後:

using System.Reflection; 
using Word = Microsoft.Office.Interop.Word; 
using Microsoft.Office.Core; 

添加此helper方法(它處理運行宏) :

private void RunMacro(object oApp, object[] oRunArgs) 
{ 
    oApp.GetType().InvokeMember("Run", 
    System.Reflection.BindingFlags.Default | 
    System.Reflection.BindingFlags.InvokeMethod, 
    null, oApp, oRunArgs); 
} 

然後調用y我們的宏觀使用以下(您將需要修改這個以滿足您的特定需求):

private void CallCustomHeaderFooterMacro(string filename, string pageTitle, string author) 
{ 
    Word.ApplicationClass oWord = new Word.ApplicationClass(); 
    oWord.Visible = false; 
    Word.Documents oDocs = oWord.Documents; 
    object oFile = filename; 

    // Used for optional arguments 
    object oMissing = System.Reflection.Missing.Value; 

    // CHOOSE THE APPROPRIATE CALL, DEPENDING ON THE LIBRARY YOU REFERENCE 
    // Microsoft Word 10.0 Object Library 
    Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

    // Microsoft Word 11.0 Object Library 
    // Word._Document oDoc = oDocs.Open(ref oFile, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 

    // Run the 'CustomHeaderFooter' Macro. 
    /* Change "CustomHeaderFooter" to your macro name. 
    * You can send parameters to your macro such as: 
    * a Page title, author, date or other data you need to create the customised 
    * header and footer. You add and remove these as required. I have used pageTitle 
    * and author as an example 
    */ 
    RunMacro(oWord, new Object[]{"CustomHeaderFooter", pageTitle, author}); 

    // Save (as required) 

    // Quit Word and clean up. 
    oDoc.Close(ref oMissing, ref oMissing, ref oMissing); 
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oDoc); 
    oDoc = null; 
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oDocs); 
    oDocs = null; 
    oWord.Quit(ref oMissing, ref oMissing, ref oMissing); 
    System.Runtime.InteropServices.Marshal.ReleaseComObject (oWord); 
    oWord = null; 
} 

,如果你有設置一個宏,像這樣的,你的文檔中這將工作。

Public Sub CustomHeaderFooter(sPageTitle As String, sAuthor As String) 
    ' Your code to create the header and footer as required. 
    ' The easiest way to get the macro code is to open the base document and 
    ' and record the actions that you need such as adjusting the print margins 
    ' and adding the custom headers and footers, then modify the resultant code 
    ' to accept your parameters. 
End Sub 

在你的ASP.NET,你會打電話(調整到您的文件路徑和相應參數):

CallCustomHeaderFooterMacro(Server.MapPath("~/mydocument.docx"), "Sample Title", "John Smith"); 

我希望這是顯而易見的。顯然,我不能提供宏代碼來生成實際的頁眉和頁腳,但是如上所述,最簡單的方法是記錄操作並手動調整生成的代碼。


編輯:正如一個方面說明。 Microsoft不建議從服務器端應用程序使用Office自動化,即從ASP.NET調用。 This article在這裏解釋,並提供操作Office文檔的替代機制,這些機制對您可能有用也可能沒有用。但我認爲這可能會有所幫助,具體取決於您的項目需要的可擴展性。

+0

謝謝先生!!我想先實施它,然後再說謝謝。 –

+0

非常好,很高興有幫助。 – Scott

-1

我不認爲你可以從asp.net做到這一點。但是,也許你可以創建一個VBA,當Word打開並且文件名等於由asp.net導出創建的相同文件名時運行?