资讯 | 安全 | 论坛 | 下载 | 读书 | 程序开发 | 数据库 | 系统 | 网络 | 电子书 | 站长学院 | 源码 | QQ | 专栏 | 考试 | 手册 | 软件开发| web前端| Web开发| 移动开发| 综合编程| 首页 > 程序开发 > 软件开发 > C# > 正文 C#打开Word 2013-04-02 15:32:54 我来说两句 作者:caiye917015406收藏 我要投稿 Word中几个重要的对象介绍: Application对象表示 Microsoft Office Word 2003应用程序本身。每次编写代码时,都应从Application对象开始。可以从Application对象访问Word公开的所有其他对象和集合,以及Application对象自身的属性和方法。 在Word中处理某个特定文档时,这个文档就称为活动文档,并且可通过Application对象的ActiveDocument属性引用。所有的Word Document对象同时也是Application对象的Documents集合的成员,该集合由所有打开的文档组成。使用Document对象时,允许使用单个文档,而Documents集合则允许使用所有打开的文档。由于在Application和Document级别都可以进行文档操作,所以Application和Document类共享许多成员。 Selection对象表示 Microsoft Office Word 2003 文档中当前选定的区域。在Word用户界面中执行某项操作(例如,对文本进行加粗)时,应首先选择或突出显示目标文本,然后应用格式设置。可在代码中以相同的方式使用Selection对象:先定义Selection,然后执行操作。可以使用Selection对象选择、操作和打印文档中的文本以及设置文本的格式。Selection对象始终存在于文档中。如果未选中任何对象,它表示插入点。因此,在尝试使用Selection对象执行任何操作之前,知道该对象包含哪些内容是很重要的。 Range对象与Selection对象共享着很多成员。二者之间的主要区别在于:Selection对象始终在用户界面中返回对所选内容的引用,而Range对象允许在用户界面中不显示范围的情况下处理文本。Range对象的主要优势有: Range 对象通常只需要较少行数的代码就能完成给定任务。 Range 对象不会引起与 Word 必须移动或改变活动文档中的突出显示相关的系统开销。 Range 对象比 Selection 对象的功能更强。 Bookmark对象与Range和Selection对象类似,因为它表示文档中的连续区域,既有起始位置也有结束位置。书签用于在文档中标记一个位置,或者用作文档中的文本容器。Bookmark对象可以小到只有一个插入点,也可以大到整篇文档。您还可以在文档中定义多个书签。可以将Bookmark看作是保存在文档中的一个指定位置。 打开Word: Word.Application wapp = new Microsoft.Office.Interop.Word.Application(); wapp.Visible = true; wapp = null; 打开一个现有的Word文档: Word.Application wapp = new Microsoft.Office.Interop.Word.Application(); wapp.Visible = true; object filename = "E:\\Task.doc"; object isread = false; object isvisible = true; object miss = System.Reflection.Missing.Value; wapp.Documents.Open(ref filename, ref miss, ref isread, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref miss, ref isvisible, ref miss, ref miss, ref miss, ref miss); wapp = null; 打开一个新文档: Word.Application wapp = new Microsoft.Office.Interop.Word.Application(); Word.Document adoc = new Microsoft.Office.Interop.Word.Document(); wapp.Visible = true; object miss = System.Reflection.Missing.Value; adoc = wapp.Documents.Add(ref miss, ref miss, ref miss, ref miss); wapp = null; 第二篇是具体的实例 [csharp] OpenFileDialog opd = new OpenFileDialog(); opd.InitialDirectory = "G:\\项目\\福沃德\\Project\\文本相似度检测\\data\\";; opd.Filter = "Word文档(*.doc)|*.doc|文本文档(*.txt)| *.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*"; opd.FilterIndex = 1; if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0) { //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示 //Word.ApplicationClass app = new Word.ApplicationClass(); Word.Application app = new Microsoft.Office.Interop.Word.Application(); Word.Document doc = null; object missing = System.Reflection.Missing.Value; object FileName = opd.FileName; object readOnly = false; object isVisible = true; object index = 0; try { doc = app.Documents.Open( ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); //从剪切板获取数据 IDataObject data=Clipboard.GetDataObject(); this.richTextBox1.Text= data.GetData(DataFormats.Text).ToString(); } finally { if (doc != null) { doc.Close(ref missing, ref missing, ref missing); doc = null; } if (app != null) { app.Quit(ref missing, ref missing, ref missing); app = null; } } } //end if OpenFileDialog opd = new OpenFileDialog(); opd.InitialDirectory = "G:\\项目\\福沃德\\Project\\文本相似度检测\\data\\";; opd.Filter = "Word文档(*.doc)|*.doc|文本文档(*.txt)| *.txt|RTF文档(*.rtf)|*.rtf|所有文档(*.*)|*.*"; opd.FilterIndex = 1; if (opd.ShowDialog() == DialogResult.OK && opd.FileName.Length > 0) { //建立Word类的实例,缺点:不能正确读取表格,图片等等的显示 //Word.ApplicationClass app = new Word.ApplicationClass(); Word.Application app = new Microsoft.Office.Interop.Word.Application(); Word.Document doc = null; object missing = System.Reflection.Missing.Value; object FileName = opd.FileName; object readOnly = false; object isVisible = true; object index = 0; try { doc = app.Documents.Open( ref FileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref isVisible, ref missing, ref missing, ref missing, ref missing); doc.ActiveWindow.Selection.WholeStory(); doc.ActiveWindow.Selection.Copy(); //从剪切板获取数据 IDataObject data=Clipboard.GetDataObject(); this.richTextBox1.Text= data.GetData(DataFormats.Text).ToString(); } finally { if (doc != null) { doc.Close(ref missing, ref missing, ref missing); doc = null; } if (app != null) { app.Quit(ref missing, ref missing, ref missing); app = null; } } } //end if 要注意引用 using Word = Microsoft.Office.Interop.Word; 三 相关的函数拓展,本人未亲测 创建新Word: object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); 打开文档: object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:\CCCXCXX\TestDoc.doc"; oDoc = oWord.Documents.Open(ref fileName, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); 导入模板 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; object fileName = @"E:\XXXCCX\Test.doc"; oDoc = oWord.Documents.Add(ref fileName, ref oMissing, ref oMissing, ref oMissing); .添加新表 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); .表插入行 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); .单元格合并 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2)); .单元格分离 object oMissing = System.Reflection.Missing.Value; Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing); object start = 0; object end = 0; Word.Range tableLocation = oDoc.Range(ref start, ref end); oDoc.Tables.Add(tableLocation, 3, 4, ref oMissing, ref oMissing); Word.Table newTable = oDoc.Tables[1]; object beforeRow = newTable.Rows[1]; newTable.Rows.Add(ref beforeRow); Word.Cell cell = newTable.Cell(1, 1); cell.Merge(newTable.Cell(1, 2)); object Rownum = 2; object Columnnum = 2; cell.Split(ref Rownum, ref Columnnum); 通过段落控制插入 object oMissing = System.Reflection.Missing.Value; object oEndOfDoc = ""; /**//* \endofdoc is a predefined bookmark */ //Start Word and create a new document. Word._Application oWord; Word._Document oDoc; oWord = new Word.Application(); oWord.Visible = true; oDoc = oWord.Documents.Add(ref oMissing, ref oMissing, ref oMissing, ref oMissing); //Insert a paragraph at the beginning of the document. Word.Paragraph oPara1; oPara1 = oDoc.Content.Paragraphs.Add(ref oMissing); oPara1.Range.Text = "Heading 1"; oPara1.Range.Font.Bold = 1; oPara1.Format.SpaceAfter = 24; //24 pt spacing after paragraph. oPara1.Range.InsertParagraphAfter();
C#打开Word
时间: 2024-10-09 14:59:10