Microsoft.Office.Interop.Word 对word 的基本操作

88 篇文章 0 订阅
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
using System.Reflection;
using Word = Microsoft.Office.Interop.Word;
using Microsoft.Office.Interop.Word;


    public class ClassWord
    {

        /// <SUMMARY></SUMMARY>   
        /// word 应用对象   
        ///    
        private Word.Application _wordApplication;

        /// <SUMMARY></SUMMARY>   
        /// word 文件对象   
        ///    
        private Word.Document _wordDocument;
        /// <SUMMARY></SUMMARY>   
        /// 创建文档   
        ///    
        public void CreateAWord()
        {
            //实例化word应用对象   
            this._wordApplication = new Word.Application();
            Object myNothing = System.Reflection.Missing.Value;

            this._wordDocument = this._wordApplication.Documents.Add(ref myNothing, ref myNothing, ref myNothing, ref myNothing);
        }
        /// <SUMMARY></SUMMARY>   
        /// 添加页眉   
        ///    
        /// <PARAM name="pPageHeader" />   
        public void SetPageHeader(string pPageHeader)
        {
            //添加页眉   
            this._wordApplication.ActiveWindow.View.Type = Word.WdViewType.wdOutlineView;
            this._wordApplication.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekPrimaryHeader;
            this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
            //设置中间对齐   
            this._wordApplication.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            //跳出页眉设置   
            this._wordApplication.ActiveWindow.View.SeekView = Word.WdSeekView.wdSeekMainDocument;
        }
        /// <SUMMARY></SUMMARY>   
        /// 插入文字   
        ///    
        /// <PARAM name="pText" />文本信息   
        /// <PARAM name="pFontSize" />字体打小   
        /// <PARAM name="pFontColor" />字体颜色   
        /// <PARAM name="pFontBold" />字体粗体   
        /// <PARAM name="ptextAlignment" />方向   
        public void InsertText(string pText, int pFontSize, Word.WdColor pFontColor, int pFontBold, Word.WdParagraphAlignment ptextAlignment)
        {
            //设置字体样式以及方向   
            this._wordApplication.Application.Selection.Font.Size = pFontSize;
            this._wordApplication.Application.Selection.Font.Bold = pFontBold;
            this._wordApplication.Application.Selection.Font.Color = pFontColor;
            this._wordApplication.Application.Selection.ParagraphFormat.Alignment = ptextAlignment;
            this._wordApplication.Application.Selection.TypeText(pText);
        }


        /// <SUMMARY></SUMMARY>   
        /// 换行   
        ///    
        public void NewLine()
        {
            //换行   
            this._wordApplication.Application.Selection.TypeParagraph();
        }
        /// <SUMMARY></SUMMARY>   
        /// 插入一个图片   
        ///    
        /// <PARAM name="pPictureFileName" />   
        public void InsertPicture(string pPictureFileName)
        {
            object myNothing = System.Reflection.Missing.Value;
            //图片居中显示   
            this._wordApplication.Selection.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            this._wordApplication.Application.Selection.InlineShapes.AddPicture(pPictureFileName, ref myNothing, ref myNothing, ref myNothing);
        }
        /// <SUMMARY></SUMMARY>   
        /// 保存文件    
        ///    
        /// <PARAM name="pFileName" />保存的文件名   
        public void SaveWord(string pFileName)
        {
            object myNothing = System.Reflection.Missing.Value;
            object myFileName = pFileName;
            object myWordFormatDocument = Word.WdSaveFormat.wdFormatDocument;
            object myLockd = false;
            object myPassword = "";
            object myAddto = true;
            try
            {
                   this._wordDocument.SaveAs(ref myFileName, ref myWordFormatDocument, ref myLockd, ref myPassword, ref myAddto, ref myPassword,
                    ref myLockd, ref myLockd, ref myLockd, ref myLockd, ref myNothing, ref myNothing, ref myNothing,
                    ref myNothing, ref myNothing, ref myNothing);
                   this._wordDocument.Close(ref myNothing, ref myNothing, ref myNothing);
                   this._wordApplication.Quit(ref myNothing,ref myNothing,ref myNothing);
            }
            catch
            {
                         throw new Exception("导出word文档失败!");
            }
        }   

    }

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bingo_BIG

你的鼓励是我最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值