Page tree

Versions Compared

Key

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

...

ExcelTemplate vs. ExcelApplication

Info

ExcelTemplate is the main class for template-driven document generation. This object opens an ExcelWriter template file, populates it with data from a specified data source, and generates a new Excel workbook. An Excel file uploaded as an ExcelTemplate object is not directly modifiable. ExcelApplication is the main class for pure code-based workbook generation. This class is an engine used to open, create, and write (save or stream to a browser) workbooks. A single instance of ExcelApplication can generate multiple Excel workbooks.

This tutorial opens an Excel file formatted as an ExcelTemplate origin file (i.e. it contains formatted data markers) using ExcelApplication object. This means that the file can be copied based upon the user's specification using the ExcelApplication CopySheet method. This method copies a worksheet (including its formatted data markers) to another location in the workbook. The method has three parameters:

...

Info

If you are following along and intend to use AdventureWorks2008R2, you will also want to make a reference to System.Data.SqlClient namespace. To do so, type into the code behind

Code Block
languagevb
using System.Data.SqlClient;

...

Code Block
languagevb
ExcelApplication xla = new ExcelApplication();

3. Open the ExcelApplication object with the ExcelApplication.Open() method and instantiate it as a Workbook object.

...

4. Assuming that you taking the selections from a ASP.NEt NET ListBox, using use the following utility method , to create a string list of all the countries that were selected.

Code Block
        protected List<string> GetListBoxSelections(){
        //Get the ListBox selections and make the appropriate number of copies
            List<string> countryNames = new List<string>();
            foreach (int i in ListBox1.GetSelectedIndices())
            {
                countryNames.Add(ListBox1.Items[i].Text);
            }
            return countryNames;
        }

...

Code Block
wb.Worksheets[1].Select();

8. Instantiate a new ExcelTemplate object.

Code Block
ExcelTemplate xlt = new ExcelTemplate();

9. Open the ExcelApplication workbook using the ExcelTemplate.Open(ExcelApplication, Workbook) method.

Code Block
xlt.Open(xla, wb);