c# wordOperationClass

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Word = Microsoft.Office.Interop.Word;
using System.Drawing;

namespace WordDocumentSocket
{
   
    class ReadWordData
    {
        Word.Application app = new Microsoft.Office.Interop.Word.Application(); //可以打开word程序
        Word.Document doc = null;  //一会要记录word打开的文档
        //word文档和word程序可不是一回事奥!

        //public override void openFile(object fileName) { } //打开文档
        //public override object readPar(int i) { } //读取word文档的第i段
        //public override int getParCount() { } //返回word文档一共几段
        //public override void closeFile() { }  //关闭文档
        //public override void quit() { }  //关闭word程序

        从网页上拷贝的目录有时候会出现手动换行符^l,,先将其换成回车段落标记,才能正确读取
        //public void replaceChar() { }

        /// <summary>
        /// 打开文档
        /// </summary>
        /// <param name="fileName">文件名</param>
        public void OpenFile(object fileName)
        {
            try
            {
                if (app.Documents.Count > 0)
                {
                    if (MessageBox.Show("已经打开了一个word文档,你想关闭重新打开该文档吗?", "提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
                    {
                        object unknow = Type.Missing;
                        doc = app.ActiveDocument;
                        if (MessageBox.Show("你想保存吗?", "保存", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            app.ActiveDocument.Save();
                        }

                        app.ActiveDocument.Close(ref unknow, ref unknow, ref unknow);
                        app.Visible = false;
                    }
                    else
                    {
                        return;
                    }
                }
            }
            catch (Exception)
            {
                //MessageBox.Show("您可能关闭了文档");
                app = new Microsoft.Office.Interop.Word.Application();
            }

            try
            {
                object unknow = Type.Missing;
                app.Visible = true;
                doc = app.Documents.Open(ref fileName,
                                         ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                                         ref unknow, ref unknow, ref unknow, ref unknow, ref unknow,
                                         ref unknow, ref unknow, ref unknow, ref unknow, ref unknow);
            }
            catch (Exception ex)
            {
                MessageBox.Show("出现错误:" + ex.ToString());
            }  
          
        }

        /// <summary>
        /// 读文档
        /// </summary>
        /// <param name="i">读取word文档的第i段</param>
        /// <returns></returns>
        public object ReadPar(int i)
        {
            //Word.Application newApp = new Word.Application();
            try
            {
                //if (doc.InlineShapes[i].Type == Word.WdInlineShapeType.wdInlineShapePicture)
                //{
                //    //doc.InlineShapes[i].Range.Font.b
                //    doc.InlineShapes[i].Select();
                //    //newApp.Selection.Copy();
                //    doc.InlineShapes[i].Range.Copy();
                //    Image image = Clipboard.GetImage();
                //    Bitmap bitmap = new Bitmap(image);
                //    //bitmap.Save("D:\\pic" + i + ".jpg");
                //    return 1;
                //}
                //else
                //{
                //    string temp = doc.Paragraphs[i].Range.Text.Trim();
                //    return temp;
                //}
                doc.Paragraphs[i].Range.Select();
                doc.Paragraphs[i].Range.Copy();
                //doc.InlineShapes[i].Select();
                //doc.InlineShapes[i].Range.Copy();
                //return doc.Content.Text;
                //Clipboard.Clear();
                return 1;
            }
            catch (Exception e)
            {
                MessageBox.Show("Error:"+e.ToString());
                return null;
            }
        }

        //public bool CopyWord()
        //{
        //    doc.Select();
           
        //}

        /// <summary>
        /// 返回word文档一共几段
        /// </summary>
        /// <returns></returns>
        public int GetParCount()
        {
            return doc.Paragraphs.Count;
        }

        /// <summary>
        /// 返回word文档一共几名
        /// </summary>
        /// <returns></returns>
        public int GetInlCount()
        {
            return doc.InlineShapes.Count;
        }

        /// <summary>
        /// 关闭文档
        /// </summary>
        public void CloseFile()
        {
            try
            {
                //Clipboard.Clear();
                object unknow = Type.Missing;
                object saveChanges = Word.WdSaveOptions.wdPromptToSaveChanges;
                app.ActiveDocument.Close(ref saveChanges, ref unknow, ref unknow);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error:" + ex.ToString());
            }
            //app = new Microsoft.Office.Interop.Word.Application();
        }

        /// <summary>
        /// 关闭word程序
        /// </summary>
        public void QuitWord()
        {
            try
            {
                Clipboard.Clear();
                object unknow = Type.Missing;
                object saveChanges = Word.WdSaveOptions.wdSaveChanges;
                app.Quit(ref saveChanges, ref unknow, ref unknow);
            }
            catch (Exception)
            {

            }
        }

        /// <summary>
        /// 从网页上拷贝的目录有时候会出现手动换行符^l,,先将其换成回车段落标记,才能正确读取
        /// </summary>
        public void ReplaceChar()
        {
            try
            {
                object replaceAll = Word.WdReplace.wdReplaceAll;
                object missing = Type.Missing;

                app.Selection.Find.ClearFormatting();
                app.Selection.Find.Text = "^l";

                app.Selection.Find.Replacement.ClearFormatting();
                app.Selection.Find.Replacement.Text = "^p";

                app.Selection.Find.Execute(
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref replaceAll, ref missing, ref missing, ref missing, ref missing);
            }
            catch (Exception e)
            {
                MessageBox.Show("文档出现错误,请重新操作");
            }
        }

    }
}

转载于:https://www.cnblogs.com/xsmhero/archive/2009/04/16/1436895.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值