...
- Create an instance of ExcelTemplate, for example:
Code Block c# c# title C# ExcelTemplate xlt = new ExcelTemplate();
Code Block vb.net vb.net title VB.NET Dim xlt As New ExcelTemplate()
- Call ExcelTemplate.Open() to open a template Excel file, for example:
Code Block c# c# title C# xlt.Open(Application["templatepath"] + @"\DataBinding\StringBindingTemplate.xls");
The Open method takes the file path and name of the template .xls file to open.Code Block vb.net vb.net title VB.NET xlt.Open(Application("templatepath") & _ "\DataBinding\StringBindingTemplate.xls")
# Use the ExcelTemplate.BindCellData method to assign data sources to bind to the template's data markers, for example:Code Block c# c# title C# //--- Bind the variables to the template data markers //--- %%=$RecipientName and %%=$RecipientCompany xlt.BindCellData("J. Smith", "RecipientName", xlt.CreateDataBindingProperties()); xlt.BindCellData("SoftArtisans", "RecipientCompany", xlt.CreateDataBindingProperties());
Code Block vb.net vb.net title VB.NET '--- Bind the variables to the template data markers '--- %%=$RecipientName and %%=$RecipientCompany xlt.BindCellData("J. Smith", "RecipientName", xlt.CreateDataBindingProperties()) xlt.BindCellData("SoftArtisans", "RecipientCompany", xlt.CreateDataBindingProperties())
- 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.
Scrollbar |
---|