代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
CSharp
】
Csharp Word 插入頁眉頁腳,自動頁碼
作者:
Dezai.CN
/ 发布于
2012/6/4
/
893
Csharp Word 插入頁眉頁腳,自動頁碼
<div> <div class="line number1 index0 alt2">"csharp keyword">using "csharp plain">Microsoft.Office.Interop.Word; "csharp comments">//ver 12.0</div> <div class="line number1 index0 alt2"></div> <div class="line number1 index0 alt2">"csharp comments"> <div class="container"> <div class="line number1 index0 alt2">"csharp color1">/// <summary></div> <div class="line number2 index1 alt1">"csharp spaces"> "csharp color1">/// 自動頁碼,頁眉頁腳</div> <div class="line number3 index2 alt2">"csharp spaces"> "csharp color1">/// 20120603 塗聚文</div> <div class="line number4 index3 alt1">"csharp spaces"> "csharp color1">/// Geovin Du</div> <div class="line number5 index4 alt2">"csharp spaces"> "csharp color1">/// </summary></div> <div class="line number6 index5 alt1">"csharp spaces"> "csharp color1">/// <param name="pPageHeader">頁眉自定義文字</param></div> <div class="line number7 index6 alt2">"csharp spaces"> "csharp color1">/// <param name="pPageFoot">頁腳自定義文字</param></div> <div class="line number8 index7 alt1">"csharp spaces"> "csharp keyword">public "csharp keyword">void "csharp plain">SetPageHeaderAndFooter("csharp keyword">string "csharp plain">pPageHeader, "csharp keyword">string "csharp plain">pPageFoot)</div> <div class="line number9 index8 alt2">"csharp spaces"> "csharp plain">{</div> <div class="line number10 index9 alt1">"csharp spaces"> "csharp plain">Object missing = System.Reflection.Missing.Value;</div> <div class="line number11 index10 alt2">"csharp spaces"> "csharp comments">//1 </div> <div class="line number12 index11 alt1">"csharp spaces"> "csharp comments">//添加页眉 </div> <div class="line number13 index12 alt2">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;</div> <div class="line number14 index13 alt1">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;</div> <div class="line number15 index14 alt2">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);</div> <div class="line number16 index15 alt1">"csharp spaces"> "csharp color1">////设置中间对齐 </div> <div class="line number17 index16 alt2">"csharp spaces"> "csharp comments">//this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;</div> <div class="line number18 index17 alt1">"csharp spaces"> "csharp color1">////跳出页眉设置 </div> <div class="line number19 index18 alt2">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;</div> <div class="line number20 index19 alt1"></div> <div class="line number21 index20 alt2">"csharp spaces"> "csharp comments">//_wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsMessageBox;</div> <div class="line number22 index21 alt1">"csharp spaces"> "csharp comments">//頁腳</div> <div class="line number23 index22 alt2">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekEvenPagesFooter;</div> <div class="line number24 index23 alt1">"csharp spaces"> "csharp comments">//this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);</div> <div class="line number25 index24 alt2"></div> <div class="line number26 index25 alt1"></div> <div class="line number27 index26 alt2">"csharp spaces"> "csharp comments">//<a href="http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx">http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx</a></div> <div class="line number28 index27 alt1">"csharp spaces"> "csharp comments">//2.设置要插入到文档各部分主页脚中的文本的字体。此代码示例使用活动文档。</div> <div class="line number29 index28 alt2">"csharp spaces"> "csharp comments">//<a href="http://msdn.microsoft.com/zh-cn/library/ms178795">http://msdn.microsoft.com/zh-cn/library/ms178795</a>(v=vs.90).aspx</div> <div class="line number30 index29 alt1">"csharp spaces"> "csharp keyword">foreach "csharp plain">(Microsoft.Office.Interop.Word.Section wordSection "csharp keyword">in "csharp keyword">this"csharp plain">._wordDocument.Sections)</div> <div class="line number31 index30 alt2">"csharp spaces"> "csharp plain">{</div> <div class="line number32 index31 alt1">"csharp spaces"> "csharp plain">wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;</div> <div class="line number33 index32 alt2">"csharp spaces"> "csharp plain">wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;</div> <div class="line number34 index33 alt1">"csharp spaces"> "csharp plain">wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;</div> <div class="line number35 index34 alt2">"csharp spaces"> "csharp plain">wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;</div> <div class="line number36 index35 alt1"></div> <div class="line number37 index36 alt2">"csharp spaces"> "csharp comments">//2.将文本插入到页脚中</div> <div class="line number38 index37 alt1">"csharp spaces"> "csharp plain">wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageFoot;</div> <div class="line number39 index38 alt2">"csharp spaces"> "csharp plain">wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageHeader;</div> <div class="line number40 index39 alt1">"csharp spaces"> "csharp plain">}</div> <div class="line number41 index40 alt2">"csharp spaces"> "csharp comments">//1.添加“自动图文集”项,以在文档的每个页眉中显示“第 X 页,共 Y 页</div> <div class="line number42 index41 alt1">"csharp spaces"> "csharp comments">//foreach (Microsoft.Office.Interop.Word.Section section in this._wordDocument.Sections)</div> <div class="line number43 index42 alt2">"csharp spaces"> "csharp comments">//{</div> <div class="line number44 index43 alt1">"csharp spaces"> "csharp comments">// object fieldEmpty = Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty;</div> <div class="line number45 index44 alt2">"csharp spaces"> "csharp comments">// object autoText = "AUTOTEXT \"Page X of Y\" ";</div> <div class="line number46 index45 alt1">"csharp spaces"> "csharp comments">// object preserveFormatting = true;</div> <div class="line number47 index46 alt2"></div> <div class="line number48 index47 alt1">"csharp spaces"> "csharp comments">// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(</div> <div class="line number49 index48 alt2">"csharp spaces"> "csharp comments">// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,</div> <div class="line number50 index49 alt1">"csharp spaces"> "csharp comments">// ref fieldEmpty, ref autoText, ref preserveFormatting);</div> <div class="line number51 index50 alt2"></div> <div class="line number52 index51 alt1">"csharp spaces"> "csharp comments">// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]</div> <div class="line number53 index52 alt2">"csharp spaces"> "csharp comments">// .Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;</div> <div class="line number54 index53 alt1">"csharp spaces"> "csharp comments">//}</div> <div class="line number55 index54 alt2"></div> <div class="line number56 index55 alt1"></div> <div class="line number57 index56 alt2"></div> <div class="line number58 index57 alt1">"csharp spaces"> "csharp comments">//自动页码设置</div> <div class="line number59 index58 alt2">"csharp spaces"> "csharp comments">//<a href="<a href="http://social.msdn.microsoft.com/profile/john%20yang%20-%20msft/?ws=usercard-mini">http://social.msdn.microsoft.com/profile/john%20yang%20-%20msft/?ws=usercard-mini</a>" target="_blank">John Yang </a> <a href="http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8">http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8</a></div> <div class="line number60 index59 alt1">"csharp spaces"> "csharp keyword">object "csharp plain">oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; </div> <div class="line number61 index60 alt2"></div> <div class="line number62 index61 alt1">"csharp spaces"> "csharp comments">// Open up the footer in the word document</div> <div class="line number63 index62 alt2">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;</div> <div class="line number64 index63 alt1"></div> <div class="line number65 index64 alt2">"csharp spaces"> "csharp comments">// Set current Paragraph Alignment to Center</div> <div class="line number66 index65 alt1">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; "csharp comments">//.wdAlignParagraphCenter</div> <div class="line number67 index66 alt2">"csharp spaces"> </div> <div class="line number68 index67 alt1"></div> <div class="line number69 index68 alt2">"csharp spaces"> "csharp comments">// Type in 'Page '</div> <div class="line number70 index69 alt1">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.TypeText("csharp string">"Page ""csharp plain">);</div> <div class="line number71 index70 alt2"></div> <div class="line number72 index71 alt1">"csharp spaces"> "csharp comments">// Add in current page field</div> <div class="line number73 index72 alt2">"csharp spaces"> "csharp plain">Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;</div> <div class="line number74 index73 alt1">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.Fields.Add("csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.Range, "csharp keyword">ref "csharp plain">CurrentPage, "csharp keyword">ref "csharp plain">missing, "csharp keyword">ref "csharp plain">missing);</div> <div class="line number75 index74 alt2"></div> <div class="line number76 index75 alt1">"csharp spaces"> "csharp comments">// Type in ' of '</div> <div class="line number77 index76 alt2">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.TypeText("csharp string">" of ""csharp plain">);</div> <div class="line number78 index77 alt1"></div> <div class="line number79 index78 alt2">"csharp spaces"> "csharp comments">// Add in total page field</div> <div class="line number80 index79 alt1">"csharp spaces"> "csharp plain">Object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;</div> <div class="line number81 index80 alt2">"csharp spaces"> "csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.Fields.Add("csharp keyword">this"csharp plain">._wordApplication.ActiveWindow.Selection.Range, "csharp keyword">ref "csharp plain">TotalPages, "csharp keyword">ref "csharp plain">missing, "csharp keyword">ref "csharp plain">missing);</div> <div class="line number82 index81 alt1"></div> <div class="line number83 index82 alt2"></div> <div class="line number84 index83 alt1">"csharp spaces"> "csharp keyword">object "csharp plain">start = 0; </div> <div class="line number85 index84 alt2">"csharp spaces"> "csharp keyword">object "csharp plain">end = 7;</div> <div class="line number86 index85 alt1">"csharp spaces"> "csharp plain">Microsoft.Office.Interop.Word.Range rg = "csharp keyword">this"csharp plain">._wordDocument.Range("csharp keyword">ref "csharp plain">start,"csharp keyword">ref "csharp plain">end);</div> <div class="line number87 index86 alt2">"csharp spaces"> "csharp plain">rg.Select();</div> <div class="line number88 index87 alt1"></div> <div class="line number89 index88 alt2">"csharp spaces"> </div> <div class="line number90 index89 alt1">"csharp spaces"> "csharp plain">}</div> </div> </div> </div>
试试其它关键字
頁眉頁腳
同语言下
.
文件IO 操作类库
.
Check图片类型[JPEG(.jpg 、.jpeg),TIF,GIF,BMP,PNG,P
.
机器名和IP取得(IPV4 IPV6)
.
Tiff转换Bitmap
.
linqHelper
.
MadieHelper.cs
.
RegHelper.cs
.
如果关闭一个窗体后激活另一个窗体的事件或方法
.
创建日志通用类
.
串口辅助开发类
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3