C# 操作word常用方法
来源:互联网 发布:驾校预约软件 编辑:程序博客网 时间:2024/06/05 08:30
C# 操作word常用方法,欢迎大家在回复中来补充,这里先贴个从网上找的例子,我这里也还有不少使用方法,后面会以回复的形式给出。
Word2007的API:http://msdn.microsoft.com/en-us/library/bb257531(v=office.12).aspx
Word2010的API:http://msdn.microsoft.com/en-us/library/microsoft.office.interop.word(v=office.14).aspx
using System;using System.Collections.Generic;using System.Text;using Microsoft.Office.Interop.Word;using System.Windows.Forms;namespace WordHeaderFooterManager{ class WordManager { private Microsoft.Office.Interop.Word.Application myWordApp; private Document myWordDoc; private Range allRange; public WordManager() { myWordApp = new Microsoft.Office.Interop.Word.ApplicationClass(); } public void OpenWord(string fileString) { object filepath = fileString; object oMissing = System.Reflection.Missing.Value; object objWhat = WdGoToItem.wdGoToSection; object objWhich = WdGoToDirection.wdGoToAbsolute; myWordDoc = myWordApp.Documents.Open(ref filepath, 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); allRange = myWordDoc.Range(oMissing, oMissing); } public int PageCount { get { object oMissing = System.Reflection.Missing.Value; int pageCount = myWordDoc.ComputeStatistics(WdStatistic.wdStatisticPages, oMissing); return pageCount; } } public Range GetPages(int pageIndex) { object objWhat = WdGoToItem.wdGoToPage; object objWhich = WdGoToDirection.wdGoToAbsolute; object oMissing = System.Reflection.Missing.Value; object objPage = pageIndex; Range range1 = myWordDoc.GoTo(ref objWhat, ref objWhich, ref objPage, ref oMissing); Range range2 = range1.GoToNext(WdGoToItem.wdGoToPage); object objStart = range1.Start; object objEnd = range2.Start; if (range1.Start == range2.Start) objEnd = myWordDoc.Characters.Count; return myWordDoc.Range(ref objStart, ref objEnd); } public void AddPageHeaderFooter() { //设置分节符 for (int i = 1; i <= PageCount; i++) { Range range = GetPages(i); object oCollapseEnd = WdCollapseDirection.wdCollapseEnd; object oPageBreak = WdBreakType.wdSectionBreakContinuous;//分页符 range.Collapse(ref oCollapseEnd); range.InsertBreak(ref oPageBreak); range.Collapse(ref oCollapseEnd); } //取消连接到上一页 myWordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView; myWordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader; foreach (Section section in allRange.Sections) { section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false; } //设置各节的页眉页脚 allRange.Sections[1].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "包锐1"; allRange.Sections[2].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "包锐2"; allRange.Sections[3].Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "包锐3"; //设置边框样式 foreach (Section section in allRange.Sections) { section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleThinThickThinLargeGap; section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone; section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone; section.Headers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Paragraphs.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone; } myWordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; /* object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd; object oPageBreak = Word.WdBreakType.wdSectionBreakNextPage;//分页符 range.Collapse(ref oCollapseEnd); range.InsertBreak(ref oPageBreak); range.Collapse(ref oCollapseEnd); */ /* myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders[WdBorderType.wdBorderBottom].LineStyle = WdLineStyle.wdLineStyleThinThickThinLargeGap; myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders[WdBorderType.wdBorderTop].LineStyle = WdLineStyle.wdLineStyleNone; myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders[WdBorderType.wdBorderLeft].LineStyle = WdLineStyle.wdLineStyleNone; myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders[WdBorderType.wdBorderRight].LineStyle = WdLineStyle.wdLineStyleNone; */ // MessageBox.Show(myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders.Count+""); //myWordApp.Selection.HeaderFooter.Range.Paragraphs.Borders.Enable = 0;//去除边框 } public void CloseAndSave() { try { object oMissing = System.Reflection.Missing.Value; myWordDoc.Save(); myWordDoc.Close(ref oMissing, ref oMissing, ref oMissing); } catch (Exception e) { } } public void QuitApp() { object oMissing = System.Reflection.Missing.Value; myWordApp.Quit(ref oMissing, ref oMissing, ref oMissing); } }}
- C# 操作word常用方法
- C# Word常用操作
- C#操作WORD常用东东
- docx4j-操作word常用方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- c#操作word基本方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#操作Word完全方法
- C#封装Word常用操作类
- C#操作Word完全方法(转)
- C#操作Excell常用方法
- C#操作Excell常用方法
- C#反射操作常用方法
- 数位dp cf 55d
- 抽象工厂
- UINavigationBar 背景设置
- java Enum的使用方法
- POJ:1659 Frogs' Neighborhood (Havel-Hakimi定理)
- C# 操作word常用方法
- At.js实现的输入框参数提示
- FreeBSD+openvpn之ports安装过程
- Java 基础加强 - JavaBean与内省操作
- Google 的十大信条
- c++读取utf8等不同编码文件
- wdlinux的安装目录
- 人生轨迹
- JavaScript加强之自定义callback