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("文档出现错误,请重新操作");
                }
            }

        }
    }

  • 相关阅读:
    Android studio初次安装启动时弹出unable to access android sdk add-on list提示的解决方法
    SQL Server(MSSQLSERVER) 请求失败或服务未及时响应,有关详细信息,请参见事件日志或其他的适用的错误日志。
    Docker启动时提示Get Permission Denied while trying to connect解决方法
    Docker版本与centos和ubuntu环境下docker安装介绍
    菜鸟宝典之Windows Server 2012 R2上PHP、MySQL环境搭建
    Java入门指南-04 顺序、分支、循环
    Java入门指南-03 操作符与表达式
    Java入门指南-02 变量
    Java入门指南-01 基本概要说明
    JavaWeb零基础入门-02 开发环境安装
  • 原文地址:https://www.cnblogs.com/xsmhero/p/1436895.html
Copyright © 2011-2022 走看看