zoukankan      html  css  js  c++  java
  • C#操作word

    1: 
    对项目添加引用,(DOM)Microsoft Word 11.0 Object Library和(.net)Microsoft.Office.Interop.Word 11.0 
    2: 
    在程序中添加 using Word = Microsoft.Office.Interop.Word; 

    using System; 
    using System.Collections.Generic; 
    using System.ComponentModel; 
    using System.Data; 
    using System.Drawing; 
    using System.Text; 
    using System.Windows.Forms; 
    using Word = Microsoft.Office.Interop.Word; 
    namespace WindowsFormsApplication1 

        public partial class Form1 : Form 
        { 
            Word.Application app = new Microsoft.Office.Interop.Word.Application(); //可以打开word程序 
            Word.Document doc = null;  //一会要记录word打开的文档 
            public Form1() 
            { 
                InitializeComponent(); 
            } 
            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 = false ; //显示打开word窗体吗?
                    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()); 
                 }   
               
            } 
                    public   object readPar(int i)  //一段一段的读取
                            { 
                                try 
                                { 
                                    string temp = doc.Paragraphs.Range.Text.Trim(); 
                                    return temp; 
                                } 
                                catch (Exception e) { 
                                    MessageBox.Show("Error:"+e.ToString()); 
                                    return null; 
                                } 
                            } 
                    public   int getParCount() 
                            { 
                                return doc.Paragraphs.Count; 
                            } 
                    public   void closeFile() 
                            { 
                                try 
                                { 
                                    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()); 
                                } 
                            } 
                    public   void quit() 
                            { 
                                try 
                                { 
                                    object unknow = Type.Missing; 
                                    object saveChanges = Word.WdSaveOptions.wdSaveChanges; 
                                    app.Quit(ref saveChanges, ref unknow, ref unknow); 
                                } 
                                catch (Exception) 
                                { 
                                } 
                            } 
                    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("文档出现错误,请重新操作"); 
                        } 
                    } 
  • 相关阅读:
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    深度学习入门笔记(十九):卷积神经网络(二)
    用Excel表达图片如何由像素点构成
    使用you-get爬取各大网站视频
    自从有了这样的可视化报表,我们社区再没有过疫情(内含福利)
    端口测试程序
  • 原文地址:https://www.cnblogs.com/zhiji6/p/1649313.html
Copyright © 2011-2022 走看看