当前位置:编程学习 > C#/ASP.NET >>

求C# 给word插入页眉的方法

--------------------编程问答--------------------

try
    {
        Object oMissing = System.Reflection.Missing.Value;
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        object filename = filePath;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Open(ref filename, 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);

        ////添加页眉方法一:
        //WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
        //WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
        //WordApp.ActiveWindow.ActivePane.Selection.InsertAfter( "**公司" );//页眉内容

        ////添加页眉方法二:
        if (WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdNormalView ||
            WordApp.ActiveWindow.ActivePane.View.Type == WdViewType.wdOutlineView)
        {
            WordApp.ActiveWindow.ActivePane.View.Type = WdViewType.wdPrintView;
        }
        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageHeader;
        WordApp.Selection.HeaderFooter.LinkToPrevious = false;
        WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
        WordApp.Selection.HeaderFooter.Range.Text = "页眉内容";

        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekCurrentPageFooter;
        WordApp.Selection.HeaderFooter.LinkToPrevious = false;
        WordApp.Selection.HeaderFooter.Range.ParagraphFormat.Alignment = WdParagraphAlignment.wdAlignParagraphCenter;
        WordApp.ActiveWindow.ActivePane.Selection.InsertAfter("页脚内容");

        //跳出页眉页脚设置
        WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument;

        //保存
        WordDoc.Save();
        WordDoc.Close(ref oMissing, ref oMissing, ref oMissing);
        WordApp.Quit(ref oMissing, ref oMissing, ref oMissing);
        return true;
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
        Console.WriteLine(e.StackTrace);
        return false;
    }

参考http://www.cnblogs.com/lantionzy/archive/2009/10/23/1588511.html --------------------编程问答-------------------- report对象是什么? --------------------编程问答--------------------
引用 2 楼 devmiao 的回复:
report对象是什么?
string text=textBox14.Text ;
     report.SetPageHeader(text);
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,