求C# 给word插入页眉的方法 本帖最后由 u011585361 于 2013-08-04 21:37:08 编辑 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 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 string text=textBox14.Text ; report.SetPageHeader(text); 如何控制恶意信息发布 新手求教:如何实现模拟点击网页上的按钮 有没有这样的word分割工具 c# 动态加载dll时 有参构造 和 方法重载问题 QQ聊天记录 的 显示控件 请求一个sql语句!!!!! 菜鸟求救:在线等 .net中,如何给一个datetime型的数据库字段赋一个空值? 请问怎么实现label上字符的平移效果? datagrid双击双取值问题。 基于UDP的局域网语音单工通信模拟 WPF绘制云图
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.SetPageHeader(text);