...
If you are coding in the code-behind page, include a using or Imports statement at the top of the code-behind page:
Csharp | ||||
---|---|---|---|---|
| ||||
using SoftArtisans.OfficeWriter.ExcelWriter; |
...
- Create an instance of ExcelTemplate, for example:code
Csharp |
---|
...
|
...
|
...
ExcelTemplate xlt = new ExcelTemplate();
|
...
Vbnet |
---|
Dim xlt As New ExcelTemplate() |
...
- Call ExcelTemplate.Open() to open a template Excel file, for example:code
Csharp |
---|
...
|
...
|
...
xlt.Open(Application["templatepath"] +
@"\DataBinding\StringBindingTemplate.xls");
|
...
Vbnet |
---|
xlt.Open(Application("templatepath") |
...
& |
...
_ |
...
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 |
---|
...
|
...
|
...
//--- 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 |
...
|
...
Smith", |
...
|
...
|
...
|
...
|
...
|
...
- Call ExcelTemplate.Process to populate the template's data markers with data source values:
The Process method enters data source values in the template's merge fields, and creates the output file (the new spreadsheet) in memory.Code Block c# c# xlt.Process();
# Call ExcelTemplate.Save to generate a new spreadsheet:
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.Code Block c# c# xlt.Save(Page.Response, "StringBinding.xls", false);
\ExcelWriter allows you to save the generated file on the server or stream it to the client. For more information, see Output Options.