110,533
社区成员
发帖
与我相关
我的任务
分享
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