C# 操作word总结(一)——建立文档和添加页眉页脚

      最近程序中经常使用到word的操作,我在网上查了一些资料,在这里整理一下。

      使用代码创建word文档:

#region 新建Word文档
/// <summary>
/// 动态生成Word文档并填充内容 
/// </summary>
/// <param name="dir">文档目录</param>
/// <param name="fileName">文档名</param>
/// <returns>返回自定义信息</returns>
public static bool CreateWordFile(string dir, string fileName)
{
    try
    {
        Object oMissing = System.Reflection.Missing.Value;

        if (!Directory.Exists(dir))
        {
            //创建文件所在目录
            Directory.CreateDirectory(dir);
        }
        //创建Word文档(Microsoft.Office.Interop.Word)
        Microsoft.Office.Interop.Word._Application WordApp = new Application();
        WordApp.Visible = true;
        Microsoft.Office.Interop.Word._Document WordDoc = WordApp.Documents.Add(
            ref oMissing, ref oMissing, ref oMissing, ref oMissing);

        //保存
        object filename = dir + fileName;
        WordDoc.SaveAs(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);
        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;
    }
}
#endregion 新建Word文档
      给Word文档添加页眉页脚
#region 给word文档添加页眉页脚
/// <summary>
/// 给word文档添加页眉
/// </summary>
/// <param name="filePath">文件名</param>
/// <returns></returns>
public static bool AddPageHeaderFooter(string filePath)
{
    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;
    }
}
#endregion 给word文档添加页眉页脚
      在这里做个笔记,希望能帮助大家。另外 部分代码 来自于网络。


  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 39
    评论
评论 39
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值