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);        }    }}


原创粉丝点击