Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

5. The template is now complete. We'll move on to writing the code to bind the data to this merge field.

Writing the code

Info

In the sample code, the sample web application page WorldTemplate_HelloWorld.aspx and code behind WordTemplate_HelloWorld.aspx.cs/vb are available in the Hello World project.

1. Include the SoftArtisans.OfficeWriter.WordWriter namespace in the code behind.

Csharp
1
1
using SoftArtisans.OfficeWriter.WordWriter;
Vbnet
1
1

Imports SoftArtisans.OfficeWriter.WordWriter

2. Instantiate the WordTemplate object.

Csharp
2
2

WordTemplate WT = new WordTemplate();
Vbnet
2
2

3. Open the template file with WordTemplate.Open()

Csharp
3
3

WT.Open(Page.MapPath("templates\\template.docx"));
Vbnet
3
3

4. In this example, we'll pull a text value from a textbox on a web form that a user submitted. Get the data value from the web form's text box

Csharp
4
4

string value = DataValueBox.Text.Trim();
Vbnet
4
4

5. Create an object array to hold the textbox value. Create a string array to hold the column name "Variable"

Csharp
5
5

object[] values = { value };
string[] columnNames = { "Variable" }
Vbnet
5
5

6. Use WordTemplate.SetDataSource to bind the data to template file.

Csharp
6
6

WT.SetDataSource(values, columnNames);
Vbnet
6
6

WordTemplate has three methods to bind data: SetDataSource, SetRepeatBlock, and SetMailMerge. SetDataSource binds a single row of data to the template, where the merge fields can span the entire document. To bind a single value or group of single values, you need to put those in a data set, such as an array and then bind that array to the template file.

7. Call WordTemplate.Process() to bind the data to the template file.

Csharp
7
7

WT.Process();
Vbnet
7
7

8. Save the output with WordTemplate.Save().

Csharp
7
7

WT.Save(Response, "Output.docx", false);
Vbnet
7
7

There are several options for WordTemplate.Save including: 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.

Info

WordWriter does not convert between file formats, so it is important that the file extension on the output file matches the file extension of the original template file.

9. Run your code.

In the output file you will see that the merge field has been replaced by the value.

SCREEN SHOT

Congratulations, you completed Hello World using WordTemplate!

Final Code

Csharp
8
8

using SoftArtisans.OfficeWriter.WordWriter;
...
WordTemplate WT = new WordTemplate();

WT.Open(Page.MapPath("templates\\template.docx"));

string value = DataValueBox.Text.Trim();

object[] values = { value };
string[] columnNames = { "Variable" };

WT.SetDataSource(values, columnNames);

WT.Process();

WT.Save(Response, "Output.docx", false);
Vbnet
8
8

Downloads

You can download the code for the hello world Hello World tutorial as a Visual Studio solution.

...