1. 程式人生 > >C# 操作word總結(一)——建立文件和新增頁首頁尾

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文件新增頁首頁尾
      在這裡做個筆記,希望能幫助大家。另外 部分程式碼 來自於網路。