Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 4.0
Wiki Markup
|| Table of

...

Table of Contents

Hello World with ExcelApplication

 Contents ||
| {toc} |

h1. _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.

...



h1. Diving right into the code

...



{info
}In the Hello World sample web application, WordApplication_HelloWorld.aspx has a text box for users to supply a value and WordApplication_HelloWorld.aspx.cs/vb contains the code show below.
{info}
1. Include the {{SoftArtisans.OfficeWriter.WordWriter}} namespace in the code behind.

...



{csharp
:1
1
}
using SoftArtisans.OfficeWriter.WordWriter;
Vbnet
11Imports
{csharp}

{vbnet:1}
Imports SoftArtisans.OfficeWriter.WordWriter

{vbnet}

2. Instantiate the [WordApplication] object

...



{csharp
:2
2
}
WordApplication WAPP = new WordApplication();
Vbnet
22

Dim WAPP As WordApplication = new WordApplication()

...

{csharp}

{vbnet:2}
Dim WAPP As WordApplication = new WordApplication()
{vbnet}

3. Create a new document with [WordApplication.Create()

...

Csharp
33
]

{csharp:3}
Document DOC = WAPP.Create();
Vbnet
33Dim DOC As Document =
{csharp}

{vbnet:3}
Dim DOC As Document = WAPP.Create()

...


{vbnet}

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.

...



{note
}WordApplication only supports the Office 2003 file formats - DOC, DOT. WordWriter does not currently support creating DOCX/DOCM files with WordApplication.
{note}

4. For this example, we'll pull a value from a textbox in a web form. Get the value.

...



{csharp
:4
4
}
string value = DataValueBox.Text.Trim();
Vbnet
44Dim value As string =
{csharp}

{vbnet:4}
Dim value As string = DataValueBox.Text.Trim()

{vbnet}

5. Insert the text into the document with

...

Csharp
55
 [InsertTextAfter|Element.InsertTextAfter]

{csharp:5}
DOC.InsertTextAfter(value, true);
Vbnet
55
Csharp
66
{csharp}

{vbnet:5}
DOC.InsertTextAfter(value, true)

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.

6. Save the output file with WordApplication.Save


{vbnet}

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.

6. Save the output file with [WordApplication.Save]

{csharp:6}
WAPP.Save(DOC, Response, "Output.doc", false);
Vbnet
66
{csharp}

{vbnet:6}
WAPP.Save(DOC, Response, "Output.doc", false)

{vbnet}

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:

...

Image Removed



!OutputWAPP.png|border=1,width=700!

Congratulations, you have completed _Hello World_ using WordApplication!

...



h1. Final Code

...



{csharp
:7
7
}
using SoftArtisans.OfficeWriter.WordWriter;
...
WordApplication WAPP = new WordApplication();

Document DOC = WAPP.Create();

string value = DataValueBox.Text.Trim();

DOC.InsertTextAfter(value, true);

WAPP.Save(DOC, Response, "Output.doc", false);
Vbnet
77Imports
{csharp}

{vbnet:7}
Imports SoftArtisans.OfficeWriter.WordWriter


...


Dim WAPP As WordApplication = new WordApplication()


Dim DOC As Document= WAPP.Create()


Dim value As string = DataValueBox.Text.Trim()


DOC.InsertTextAfter(value, true)


WAPP.Save(DOC, Response, "Output.doc", false)

Downloads

You can download the code for the hello world tutorial as a visual studio solution.



{vbnet}

h1. Downloads

You can download the code for the hello world tutorial as a visual studio solution.

* [Hello World Tutorial^WordWriter_HelloWorldC#.zip]
* [Hello World Tutorial^WordWriter_HelloWorldVB.zip]