...
Csharp |
---|
|
using SoftArtisans.OfficeWriter.WordWriter;
|
Vbnet |
---|
|
Imports SoftArtisans.OfficeWriter.WordWriter |
2. Instantiate the WordTemplate object.
Csharp |
---|
|
WordTemplate WT = new WordTemplate();
|
Vbnet |
---|
|
Dim WT As WordTemplate = New WordTemplate() |
3. Open the template file with WordTemplate.Open
Csharp |
---|
|
WT.Open(Page.MapPath("templates\\template.docx"));
|
Vbnet |
---|
|
WT.Open(Page.MapPath("templates template.docx")) |
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 |
---|
|
string value = DataValueBox.Text.Trim();
|
Vbnet |
---|
|
Dim value As String = DataValueBox.Text.Trim() |
5. Create an object array to hold the textbox value. Create a string array to hold the column name "Variable"
Csharp |
---|
|
object[] values = { value };
string[] columnNames = { "Variable" }
|
Vbnet |
---|
|
Wiki Markup |
---|
Dim values() As Object = {value}
Dim columnNames() As String = {"Variable"}
|
|
6. Use WordTemplate.SetDataSource to bind the data to template file.
Csharp |
---|
|
WT.SetDataSource(values, columnNames);
|
Vbnet |
---|
|
WT.SetDataSource(values, columnNames) |
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.
...
8. Save the output with WordTemplate.Save
Csharp |
---|
|
WT.Save(Response, "Output.docx", false);
|
Vbnet |
---|
|
WT.Save(Response, "Output.docx", False) |
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.
...
Csharp |
---|
|
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 |
---|
|
Wiki Markup |
---|
Imports SoftArtisans.OfficeWriter.WordWriter
...
Dim WT As WordTemplate = New WordTemplate()
WT.Open(Page.MapPath("templates\\template.docx"))
Dim value As String = DataValueBox.Text.Trim()
Dim values() As Object = {value}
Dim columnNames() As String = {"Variable"}
WT.SetDataSource(values, columnNames)
WT.Process()
WT.Save(Response, "Output.docx", False)
|
|
Downloads
You can download the code for the Hello World tutorial as a Visual Studio solution.
...