`
lu930124
  • 浏览: 27998 次
  • 性别: Icon_minigender_2
  • 来自: 廊坊
文章分类
社区版块
存档分类
最新评论

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文档添加页眉页脚
在这里做个笔记,希望能帮助大家。另外 部分代码 来自于网络。


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics