Page tree

Versions Compared

Key

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

...

  1. Create an instance of ExcelTemplate, for example:
    Code Block
    c#
    c#
    titleC#
    ExcelTemplate xlt = new ExcelTemplate();
    
    Code Block
    vb.net
    vb.net
    titleVB.NET
    Dim xlt As New ExcelTemplate()
    
  2. Call ExcelTemplate.Open() to open a template Excel file, for example:
    Code Block
    c#
    c#
    titleC#
    xlt.Open(Application["templatepath"] +
         @"\DataBinding\StringBindingTemplate.xls");
    
    Code Block
    vb.net
    vb.net
    titleVB.NET
    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:
    Code Block
    c#
    c#
    titleC#
    //--- 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
    titleVB.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())
    
  3. 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.
Scrollbar