zoukankan      html  css  js  c++  java
  • 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("文档出现错误,请重新操作");
                }
            }

        }
    }

  • 相关阅读:
    C语言指针和数组
    C语言malloc、calloc函数
    33、二叉树的后序遍历序列
    进程、线程、协程
    8、字符串转整数
    51、数组中的逆序对
    49、丑数
    19、正则表达式匹配
    32、从上到下打印二叉树
    leetcode5:最长回文子串
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1436895.html
Copyright © 2011-2022 走看看