Table of Contents
Hello World with ExcelApplication
WordWriter's WordApplication approach provides you with full programmatic control over the binary Word file formats (DOC, DOT). This includes the ability to create and modify: paragraphs, tables, lists, hyperlinks, bookmarks, images and more! This tutorial will show you how to create a new document and write some text to that document.
Diving right into the code
Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind.
Instantiate the WordApplication object
Create a new document with WordApplication.Create()
Unlike the WordTemplate object, which represents a single file, WordApplication works as a file generation engine. The WordApplication object can be used to create, open, and save multiple documents at once.
For this example, we'll pull a value from a text box in a web form. Get the value.
Insert the text into the document with Element.InsertTextAfter.
Text strings need to be inserted into a paragraph, table cell, or list entry elements in a document. In this case, when the text is inserted into the document, it's actually inserted into the last paragraph in the document. Documents created by WordApplication come with a paragraph, so we do not need to create that paragraph.
Save the output file with WordApplication.Save
WordApplication.Save has the same output options as WordTemplate: save to disk, save to memory stream, stream back to the client inline, and stream back to the client as an attachment. In this case, we're streaming the document back to the client as an attachment.
Here is an example of what the output should look like:
Congratulations, you have completed Hello World using WordApplication!
You can download the code for the Hello World tutorial as a Visual Studio solution.