Wiki Markupexcerpt |
---|
The following example generates a Word file from a template that contains the merge fields ProductName, CompanyName, and URL. The WordWriter code gets the values for these fields from an array of objects. |
To
...
set
...
a
...
template's
...
data
...
source
...
to
...
an
...
array,
...
first
...
create
...
an
...
object
...
array
...
of
...
field
...
values
...
and
...
a
...
string
...
array
...
of
...
field
...
names:
...
Csharp |
---|
//--- Create an object array of field values
object[] arrValue = {"SoftArtisans", "WordWriter", "http://www.softartisans.com"};
//--- Create a string array of field names
string[] arrName = {"CompanyName", "ProductName", "URL"};
{csharp}
{vbnet}
|
Code Block | ||
---|---|---|
| ||
'--- Create an object array of field values.
Dim arrValue As Object() = {"SoftArtisans", "WordWriter", "http://www.softartisans.com"}
'--- Create a string array of field names.
Dim arrName As String() = {"CompanyName", "ProductName", "URL"}
{vbnet}
!use_array_as_datasource.png!
Each merge field in the template will bind to a field name/field value pair. When creating field name and field value arrays to use as a data source, remember:
* The two arrays must contain the same number of elements.
* Each name in the array of field names must be the same as the corresponding merge field name in the template.
* The number of unique merge fields in the template may not exceed the number of values in the data source. The data source must provide a value for each merge field in the template.
* The number of values in the data source may be greater than the number of merge fields in the template.
First, create the WordTemplate object and open a template file:
{code:c#|title=C#}
|
Each merge field in the template will bind to a field name/field value pair. When creating field name and field value arrays to use as a data source, remember:
- The two arrays must contain the same number of elements.
- Each name in the array of field names must be the same as the corresponding merge field name in the template.
- The number of unique merge fields in the template may not exceed the number of values in the data source. The data source must provide a value for each merge field in the template.
- The number of values in the data source may be greater than the number of merge fields in the template.
First, create the WordTemplate object and open a template file:
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
string templatePath;
WordTemplate wt = new WordTemplate();
wt.Open(templatePath);
{code}
{code: |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| |title=
| }
|||||||||
Dim templatePath As String
Dim wt As New WordTemplate()
wt.Open(templatePath)
{code}
|
You
...
can
...
call
...
the
...
SetDataSourcemethod
...
before
...
or
...
after
...
opening
...
the
...
template
...
(that
...
is,
...
before
...
or
...
after
...
calling
...
Open).
...
When
...
you
...
call
...
SetDataSource,
...
pass
...
the
...
two
...
arrays
...
you
...
created
...
to
...
the
...
method
...
as
...
parameters:
...
Code Block | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| |
| =
| }
||||||||||
wt.SetDataSource(arrValue, arrName);
{code}
{code: |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| |title=
| }
|||||||||
wt.SetDataSource(arrValue, arrName)
{code}
|
The
...
Process
...
method
...
enters
...
the
...
array
...
values
...
in
...
the
...
template's
...
merge
...
fields:
...
Code Block | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| |
| =
| }
||||||||||
wt.Process();
{code}
{code: |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| |title=
| }
|||||||||
wt.Process()
{code}
|
The
...
Save
...
either
...
saves
...
the
...
new
...
Word
...
file
...
on
...
the
...
server,
...
or
...
streams
...
it
...
to
...
the
...
browser.
...
In
...
this
...
example,
...
WordWriter
...
streams
...
the
...
generated
...
file
...
to
...
the
...
client:
...
Code Block | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
| |
| =
| }
||||||||||
wt.Save(Page.Response, "ArrayVarsOutput.doc", false);
{code}
{code: |
Code Block | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|
| |title=
| }
|||||||||
wt.Save(Page.Response, "ArrayVarsOutput.doc", False)
{code}
|
On
...
the
...
client,
...
a
...
File
...
Download
...
dialog
...
will
...
ask
...
the
...
user
...
to
...
open
...
or
...
save
...
the
...
Word
...
file.
...
Since
...
the
...
Save
...
method's
...
third
...
parameter
...
-
...
OpenInBrowser
...
-
...
is
...
set
...
to
...
False,
...
if
...
the
...
user
...
chooses
...
to
...
open
...
the
...
file,
...
it
...
will
...
open
...
in
...
Microsoft
...
Word.