0

我有一个gridview,我需要将gridview 导出到MS word,这工作正常,但我需要在Ms word 的每一页上放置自定义页眉和页脚。是否可以在导出的 gridview 上从 asp.net 运行宏,以便我可以将自定义页眉和页脚放在导出的 gridview 上,并调整打印页面上的一些边距。

任何帮助将不胜感激。

谢谢。

4

2 回答 2

1

本教程将向您展示如何使用 .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;

添加这个辅助方法(处理运行宏):

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

然后调用您的宏使用以下(您需要修改它以满足您的特定需求):

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 调用。本文在此处进行了解释,并提供了用于处理 Office 文档的替代机制,这可能对您有用,也可能对您没有用。但我认为这可能会有所帮助,具体取决于您的项目所需的可扩展性。

于 2012-09-23T19:36:27.993 回答
-1

我不认为你可以从 asp.net 做到这一点。但是您是否可以创建一个在 Word 打开且文件名与 asp.net 导出创建的相同文件名时运行的 VBA?

于 2012-09-23T18:18:37.243 回答