在ASP.NET中使用Microsoft Word文档
|
源代码网整理以下本文是应在ASP.NET里创建Microsoft Word文档之需而写的。这篇文章演示了在ASP.NET里怎么创建和修改Microsoft Word文档。 源代码网整理以下[背景] 源代码网整理以下 自动化是一种能让各种语言编写的(如:Visual Basic.NET或C#)应用程序在程序级别上控制其他应用程序。 源代码网整理以下[建立工程] 源代码网整理以下 在.NET里操作Word的第一步就是添加COM引用到你的工程里,通过右键点击Solution Explorer的Reference,Add Reference。选择COM选项卡,查找Microsoft Word 10.0 Object Library。点击选择,OK。 源代码网整理以下 Word.ApplicationClass oWordApp = new Word.ApplicationClass(); 源代码网整理以下 源代码网整理以下 1.在Tools菜单的Macro选项里选择 Record New Macro ,并且执行你有兴趣的任务。 源代码网整理以下 上面的操作产生了VBA代码来完成你记录的任务。需要注意的是,宏在大多数情况下不是最好的代码,但是它提供了一种便捷和可用的方法。 源代码网整理以下 object fileName = "c:\database\test.doc"; 源代码网整理以下 Word.Document oWordDoc = oWordApp.Documents.Open(ref fileName, ref missing,ref readOnly, 源代码网整理以下 oWordDoc.Activate(); 源代码网整理以下 oWordApp.Selection.TypeText("This is the text"); 源代码网整理以下 oWordApp.Application.Quit(ref missing, ref missing, ref missing); 软件开发网 www.mscto.com
源代码网整理以下 软件开发网 www.mscto.com
源代码网整理以下 如果创建一个新文档并保存是这样写的: 源代码网整理以下 Word.ApplicationClass oWordApp = new Word.ApplicationClass(); 源代码网整理以下 Word.Document oWordDoc = oWordApp.Documents.Add(ref missing, ref missing,ref missing, ref missing); 源代码网整理以下 oWordDoc.Activate(); 源代码网整理以下 oWordApp.Selection.TypeText("This is the text"); 源代码网整理以下 oWordApp.Application.Quit(ref missing, ref missing, ref missing); 源代码网整理以下 源代码网整理以下 object fileName = "c:\database\test.doc"; 软件开发网 www.mscto.com 源代码网整理以下 源代码网整理以下[使用摸版] 软件开发网 www.mscto.com 源代码网整理以下 如果你需要自动的建立有通用格式的文档,那你可以使用基于预格式化的摸版来建立新文档,这样可以方便很多。 源代码网整理以下 1.你可以更大程度的格式化文档和控制文档里的对象。 源代码网整理以下 通过使用摸版,你可以调整表格、段落和其他一些在文档里的对象的位置,同时包括格式化这些对象。通过使用自动化处理,你可以建立一个基于摸版的文档,代码如下: 源代码网整理以下 Word.ApplicationClass oWordApp = new Word.ApplicationClass(); 源代码网整理以下 源代码网整理以下 object oBookMark = "MyBookmark"; 源代码网整理以下 源代码网整理以下 使用摸版的另一个优点是你可以创建和保存那些在运行过程中你想要的格式化样式,如下: 源代码网整理以下 object oStyleName = "MyStyle"; 源代码网整理以下 源代码网整理以下 在工程中包含了CCWordApp.cs这个文件,我不想总是在写象插入文本,打开文档这样的代码。 源代码网整理以下 public class CCWordApp 源代码网整理以下 // Activate the interface with the COM object of Microsoft Word 源代码网整理以下 // Open an existing file or open a new file based on a template 源代码网整理以下 // Open a new document 源代码网整理以下 // Deactivate the interface with the COM object of Microsoft Word 源代码网整理以下 // Save the document 源代码网整理以下 //Save the document with a new name as HTML document 源代码网整理以下 // Save the document in HTML format 源代码网整理以下 // Insert Text 源代码网整理以下 // Insert Line Break 源代码网整理以下 // Insert multiple Line Break 源代码网整理以下 // Set the paragraph alignment 源代码网整理以下 // Set the font style 源代码网整理以下 // Disable all the style 软件开发网 www.mscto.com 源代码网整理以下 // Set the font name 源代码网整理以下 // Set the font dimension 源代码网整理以下 // Insert a page break 源代码网整理以下 // Go to a predefined bookmark 源代码网整理以下 // Go to the end of document 源代码网整理以下 // Go to the beginning of document 源代码网整理以下 打开一个存在的文件的代码将是这样的: 源代码网整理以下 CCWordApp test ; 源代码网整理以下[细节] 源代码网整理以下 演示工程包含: 源代码网整理以下 注意你用来保存文档的目录,应该是可重写的。 源代码网整理以下 源代码网推荐 源代码网供稿. |
