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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您想要将现有的 C# 项目迁移到 C# Core 吗?如果是这样,您可以按照以下步骤进行操作: 1. 首先,确保您已经安装了 .NET Core SDK,您可以在 Microsoft 的官方网站上下载并安装它。 2. 打开您的 C# 项目,并确保它在 Visual Studio 中可正常编译和运行。 3. 创建一个新的 C# Core 项目。您可以使用 Visual Studio 或者使用命令行工具(如 dotnet CLI)来完成这一步骤。 4. 将原有项目中的代码和文件复制到新的 C# Core 项目中。请注意,某些特定的功能、类库或者 API 可能需要进行适当的修改或者替换,以适应 C# Core 的要求。 5. 更新项目的依赖项。C# Core 使用 NuGet 包管理器来管理依赖项。您可以使用 Visual Studio 或者 dotnet CLI 来添加、更新或者删除项目的依赖项。 6. 对于任何特定于平台的代码,您可能需要进行一些调整。C# Core 是跨平台的,因此某些特定于平台的 API 或者功能可能需要进行适当的修改。 7. 进行测试并调试。确保您的 C# Core 项目能够正确地编译、运行和提供预期的功能。 8. 最后,根据需要,您可以部署和发布您的 C# Core 项目。您可以将其打包为一个独立的可执行文件,或者作为一个 ASP.NET Core Web 应用程序进行部署。 这些是一般的步骤,具体的迁移过程可能会因项目的复杂性和特定要求而有所不同。请确保在迁移之前备份您的现有项目,并在迁移过程中进行适当的测试和验证。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值