Page tree

Versions Compared

Key

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

...

If you are coding in the code-behind page, include a using or Imports statement at the top of the code-behind page:

Csharp
1
1
using SoftArtisans.OfficeWriter.ExcelWriter;

...

  1. Create an instance of ExcelTemplate, for example:code
Csharp

...

2

...

2

...


ExcelTemplate xlt = new ExcelTemplate();

...

Vbnet

Dim xlt As New ExcelTemplate()

...

  1. Call ExcelTemplate.Open() to open a template Excel file, for example:code
Csharp

...

3

...

3

...


xlt.Open(Application["templatepath"] +
     @"\DataBinding\StringBindingTemplate.xls");

...

Vbnet

xlt.Open(Application("templatepath")

...

&

...

_
"\DataBinding\StringBindingTemplate.xls")

...

The Open method takes the file path and name of the template .xls file to open.
# Use the ExcelTemplate.BindCellData method to assign data sources to bind to the template's data markers, for example:

...

Csharp

...

4

...

4

...


//--- Bind the variables to the template data markers
//--- %%=$RecipientName and %%=$RecipientCompany
xlt.BindCellData("J. Smith",
     "RecipientName",
     xlt.CreateDataBindingProperties());
xlt.BindCellData("SoftArtisans",
     "RecipientCompany",
     xlt.CreateDataBindingProperties());

...

Vbnet

'---

...

Bind

...

the

...

variables

...

to

...

the

...

template

...

data

...

markers

...


'---

...

%%=$RecipientName

...

and

...

%%=$RecipientCompany

...


xlt.BindCellData("J.

...

Smith",

...


"RecipientName",

...


xlt.CreateDataBindingProperties())

...


xlt.BindCellData("SoftArtisans",

...


"RecipientCompany",

...


xlt.CreateDataBindingProperties())

...

  1. Call ExcelTemplate.Process to populate the template's data markers with data source values:
    Code Block
    c#
    c#
    xlt.Process();
    
    The Process method enters data source values in the template's merge fields, and creates the output file (the new spreadsheet) in memory.
    # Call ExcelTemplate.Save to generate a new spreadsheet:
    Code Block
    c#
    c#
    xlt.Save(Page.Response, "StringBinding.xls", false);
    
    If you pass Save a Page.Response object, ExcelWriter will stream the generated file to the client. Save's second parameter specifies a name for the generated Excel file; this name will be displayed in the download dialog when the file is streamed to the browser. If the third parameter is set to true and the user chooses to open the file, the file will open in the browser window; if it is set to false (as in the example) and the user chooses to open the file, the file will open in Microsoft Excel.
    \ExcelWriter allows you to save the generated file on the server or stream it to the client. For more information, see Output Options.