logo 广告
Loading...
 导航 所在位置:论坛首页 -> ┈┋电脑网络┋┈ -> Asp/Asp.Net教程 -> C#封装Word常用操作类
回复
标题:C#封装Word常用操作类收藏 编辑 删除 楼主 | 上一篇 下一篇
你这是干嘛
头像
等级:社区游民
权限:普通用户
积分:13
金钱:195
声望:20
经验:20
发帖数:51
注册:2008年4月20日
资料 短消息2008-6-7 10:47:12
using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.Office.Interop.Word;
    using System.Diagnostics;
    namespace OfficeManager
    {
        public class WordClass : IDisposable
        {
            #region 字段
            private _Application m_WordApp = null;
            private _Document m_Document = null;
            private object missing = System.Reflection.Missing.Value;
            #endregion
            #region 构造函数与析构函数
            public WordClass()
            {
                m_WordApp = new ApplicationClass();
            }
            ~WordClass()
            {
                try
                {
                    if (m_WordApp != null)
                        m_WordApp.Quit(ref missing, ref missing, ref missing);
                }
                catch (Exception ex)
                {
                    Debug.Write(ex.ToString());
                }
            }
            #endregion
            #region 属性
            public _Document Document
            {
                get
                {
                    return m_Document;
                }
            }
            public _Application WordApplication
            {
                get
                {
                    return m_WordApp;
                }
            }
            public int WordCount
            {
                get
                {
                    if (m_Document != null)
                    {
                        Range rng = m_Document.Content;
                        rng.Select();
                        return m_Document.Characters.Count;
                    }
                    else
                        return -1;
                }
            }
            public object Missing
            {
                get
                {
                    return missing;
                }
            }
            #endregion
            #region 基本任务
            #region CreateDocument
            public void CreateDocument(string template)
            {
                object obj_template = template;
                if (template.Length <= 0) obj_template = missing;
                m_Document = m_WordApp.Documents.Add(ref obj_template, ref missing, ref missing, ref missing);
            }
            public void CreateDocument()
            {
                this.CreateDocument("");
            }
            #endregion
            #region OpenDocument
            public void OpenDocument(string fileName, bool readOnly)
            {
                object obj_FileName = fileName;
                object obj_ReadOnly = readOnly;
                m_Document = m_WordApp.Documents.Open(ref obj_FileName, ref missing, ref obj_ReadOnly, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing);
            }
            public void OpenDocument(string fileName)
            {
                this.OpenDocument(fileName, false);
            }
            #endregion
            #region Save & SaveAs
            public void Save()
            {
                if (m_Document != null)
                    m_Document.Save();
            }
            public void SaveAs(string fileName)
            {
                object obj_FileName = fileName;
                if (m_Document != null)
                {
                    m_Document.SaveAs(ref obj_FileName, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing,
                        ref missing, ref missing, ref missing, ref missing);
                }
            }
            #endregion
            #region Close
            public void Close(bool isSaveChanges)
            {
                object saveChanges = WdSaveOptions.wdDoNotSaveChanges;
                if (isSaveChanges)
                    saveChanges = WdSaveOptions.wdSaveChanges;
                if (m_Document != null)
                    m_Document.Close(ref saveChanges, ref missing, ref missing);
            }
            #endregion
            #region 添加数据
            /// <summary>
            /// 添加图片
            /// </summary>
            /// <param name="picName"></param>
            public void AddPicture(string picName)
            {
                if (m_WordApp != null)
                    m_WordApp.Selection.InlineShapes.AddPicture(picName, ref missing, ref missing, ref missing);
            }
            /// <summary>
            /// 插入页眉
            /// </summary>
            /// <param name="text"></param>
            /// <param name="align"></param>
            public void SetHeader(string text, WdParagraphAlignment align)
            {
                this.m_WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryHeader;
                this.m_WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(text); //插入文本
                this.m_WordApp.Selection.ParagraphFormat.Alignment = align;  //设置对齐方式
                this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置
            }
            /// <summary>
            /// 插入页脚
            /// </summary>
            /// <param name="text"></param>
            /// <param name="align"></param>
            public void SetFooter(string text, WdParagraphAlignment align)
            {
                this.m_WordApp.ActiveWindow.View.Type = WdViewType.wdOutlineView;
                this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekPrimaryFooter;
                this.m_WordApp.ActiveWindow.ActivePane.Selection.InsertAfter(text); //插入文本
                this.m_WordApp.Selection.ParagraphFormat.Alignment = align;  //设置对齐方式
                this.m_WordApp.ActiveWindow.View.SeekView = WdSeekView.wdSeekMainDocument; // 跳出页眉设置
            }
            #endregion
            #region Print
签名

2008-6-7 10:47:12 顶部
第1页 共页 共0个回复     <<    >>    
 快速回复
  • 支持UBB,HTML标签

  • 高级回复

  • 操作选项:评分 加精 解精 奖惩 设专题 设公告 解公告 固顶 总固顶 解固顶 结帖 解结帖 锁帖 解锁 移帖 删帖
      首页 | 购买指南 | 虚拟主机 | 特色介绍 | 下载中心 | 支付方式
    Copyright 2004-2008 BBSGood.com Powered By: BBSGood.Speed Version 5.0
      咨询电话:0575-85513832、0575-85513825(传真)、7*24小时咨询服务:13606552007 不良信息举报中心 浙ICP备05029817号
      业务QQ:38958768、客服QQ1:415896239、客服QQ2:343896043、MSN:jccsxx@hotmail.com