C# 操作word常用方法

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


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值