Microsoft Word Documents from ASP.NET
来源:互联网 发布:钉钉办公软件 编辑:程序博客网 时间:2024/05/06 05:21
http://www.codeproject.com/KB/aspnet/wordapplication.aspx
Introduction
This article is being written in response of the need of building Microsoft Word document in an ASP.NET project. This article demonstrates how to create and modify document using Microsoft Word with ASP.NET.
Background
Automation is a process that allows applications that are written in languages such as Visual Basic .NET or C# to programmatically control other applications. Automation to Word allows you to perform actions such as creating new documents, adding text to documents, mail merge, and formatting documents. With Word and other Microsoft Office applications, virtually all of the actions that you can perform manually through the user interface can also be performed programmatically by using automation. Word exposes this programmatic functionality through an object model. The object model is a collection of classes and methods that serve as counterparts to the logical components of Word. For example, there is anApplication
object, aDocument
object, and aParagraph
object, each of which contain the functionality of those components in Word.
The project
The first step in manipulating Word in .NET is that you'll need to add a COM reference to your project by right clicking in the solution explorer onReferences->Add Reference. Click on the COM tab and look for the Microsoft Word 10.0 Object Library. ClickSelect and OK.
This will automatically place an assembly in your application directory that wraps COM access to Word.
Now you can instantiate an instance of a Word application:
Word.ApplicationClass oWordApp = new Word.ApplicationClass();
You can call the interesting methods and properties that Microsoft Word provides to you to manipulate documents in Word. The best way to learn how to navigate the object models of Word, Excel, and PowerPoint is to use the Macro Recorder in these Office applications:
- Choose Record New Macro from the Macro option on the Tools menu and execute the task you're interested in.
- Choose Stop Recording from the Macro option on the Tools menu.
- Once you are done recording, choose Macros from the Macro option on theTools menu, select the macro you recorded, then clickEdit.
This takes you to the generated VBA code that accomplishes the task you recorded. Keep in mind that the recorded macro willnot be the best possible code in most cases, but it provides a quick and usable example.
For example to open an existing file and append some text:
object fileName = "c:\\database\\test.doc";object readOnly = false;object isVisible = true;object missing = System.Reflection.Missing.Value;Word.ApplicationClass oWordApp = new Word.ApplicationClass();Word.Document oWordDoc = oWordApp.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);oWordDoc.Activate();oWordApp.Selection.TypeText("This is the text");oWordApp.Selection.TypeParagraph();oWordDoc.Save();oWordApp.Application.Quit(ref missing, ref missing, ref missing);
Or to open a new document and save it:
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.Selection.TypeParagraph();oWordDoc.SaveAs("c:\\myfile.doc");oWordApp.Application.Quit(ref missing, ref missing, ref missing);
In C#, the Word Document
class's Open
method signature is defined asOpen(refobject, ref object,ref object,ref object, refobject, refobject, ref object,ref object,ref object, refobject, refobject, ref object,ref object,ref object, refobject)
. What this means is that in C# theOpen
method takes 15 required arguments, and each argument must be preceded with theref
keyword and each argument must be of typeobject
. Since the first argument is a file name, normally aString
value in Visual Basic. NET, we must declare a variable of typeobject
that holds the C#string
value, hence the code:
object fileName = "c:\\database\\test.doc";
Although we only need to use the first argument in the Open
method, remember that C# does not allow optional arguments, so we provide the final 14 arguments as variables of typeobject
that hold values ofSystem.Reflection.Missing.Value
Use a template
If you are using automation to build documents that are all in a common format, you can benefit from starting the process with a new document that is based on a preformatted template. Using a template with your Word automation client has two significant advantages over building a document from nothing:
- You can have greater control over the formatting and placement of objects throughout your documents.
- You can build your documents with less code.
By using a template, you can fine-tune the placement of tables, paragraphs, and other objects within the document, as well as include formatting on those objects. By using automation, you can create a new document based on your template with code such as the following:
Word.ApplicationClass oWordApp = new Word.ApplicationClass();object oTemplate = "c:\\MyTemplate.dot";oWordDoc = oWordApp.Documents.Add(ref oTemplate, ref Missing,ref Missing, ref Missing);
In your template, you can define bookmarks so that your automation client can fill in variable text at a specific location in the document, as follows:
object oBookMark = "MyBookmark";oWordDoc.Bookmarks.Item(ref oBookMark).Range.Text = "Some Text Here";
Another advantage to using a template is that you can create and store formatting styles that you wish to apply at run time, as follows:
object oStyleName = "MyStyle";oWordDoc.Bookmarks.Item(ref oBookMark).Range.set_Style(ref oStyleName);
- Microsoft Word Documents from ASP.NET
- Microsoft Word Documents from ASP.NET
- Microsoft Word Documents from ASP.NET
- Beginning Microsoft Word Business Documents
- Generate PDF documents from a HTML page using ASP.NET
- 从ASP.NET得到Microsoft Word文档
- Automating the Creation of Data-Rich Business Documents with Word 2007 and ASP.NET
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- 在ASP.NET中使用Microsoft Word文档
- NHibernate学习资源
- python 基于XML生成静态网页的简单实现
- MeeGo开发中一些小技巧
- 隐藏驱动完整攻略(基础篇)
- PHP5最新中文版CHM手册
- Microsoft Word Documents from ASP.NET
- 常见调试器
- shell字符串操作方法,以及实例
- WGET的使用方法与技巧
- 隐藏驱动完整攻略(猥琐篇)
- 从log文件看MeeGo播放器性能
- 用户投资SOA四大顾虑
- android多线程断点下载
- 先测试还是先代码