|
|
The ExcelTemplate Object
The ExcelTemplate object represents a template* spreadsheet, that is, a file created in
Microsoft Excel that contains data markers. A data marker specifies
a database column, variable, or array to insert in the spreadsheet column
containing the marker.
See Using Template Spreadsheets,
ExcelApp.Open vs. ExcelTemplate and
Four Ways to Use ExcelWriter for more guidance
on when and how to use the ExcelTemplate object.
| * |
In the ExcelWriter documentation, the term "template" refers to an Excel file containing
data markers, not a Microsoft Excel template (.xlt). However, ExcelWriter can
open and generate both .xls and .xlt files. Use an .xlt file the same way you would an
.xls file. |
To create an instance of the ExcelTemplate object, use,
Set objTemplate = Server.CreateObject("SoftArtisans.ExcelTemplate")
Note:
- The ExcelTemplate object supports
Excel's BIFF8 (Excel 97/2000/XP) format only; ExcelTemplate does not support BIFF7 (Excel 95)
format templates, and will not generate BIFF7 format files.
(The ExcelApplication object
can produce either Excel 97/2000/XP (BIFF8) format files
or Excel 95 (BIFF7) format files.)
- ExcelWriterFree's 500 row limit applies to templates.
ExcelTemplate Methods and Properties
| ExcelTemplate Methods and Properties |
| AnsiToUnicode V4 |
If a client submits a non-Latin alphabet string to a server
running a language different from the language of the client's HTML page,
to display the string correctly in an Excel
spreadsheet the string must be converted to Unicode.
The AnsiToUnicode method
takes a non-Latin alphabet string and its language's code page and returns a Unicode
string. If the client HTML script's charset property is set to the string's
language or to UTF-8, and the string is converted to Unicode using AnsiToUnicode,
the string will be displayed correctly in the generated spreadsheet.
For a complete list of charset and code page values see,
Character Set Recognition.
Example:
The following lines get a Hebrew string from an HTML form, convert the string to
Unicode, and insert the string at the FirstName data marker. The example
uses a specific language code page; to correctly display strings in any language, use
code page 65001.
<%
Set xlw = Server.CreateObject("Softartisans.ExcelTemplate")
...
HebrewString = xlw.ansitounicode(Request.Form("FirstName"), 1255)
xlw.Datasource("FirstName") = HebrewString
...
%>
Top |
| ContentType V4 |
Sets or returns the generated spreadsheet's
MIME content type. In most browsers, Microsoft Excel
files are mapped to the MIME type "application/vnd.ms-excel" and ExcelWriter's ContentType
is set to this value by default. However, some browsers map Excel files to a different
MIME content type, and the browser will not open a file of type "application/vnd.ms-Excel."
In this case, set ContentType to the appropriate MIME content type.
In Internet Explorer, to find out what MIME content type maps to Excel files:
- Open the Start menu and select Run.
- Entire regedit to open the Registry Editor.
- Open HKEY_CLASSES_ROOT\MIME\Database\Content Type. This folder contains
a list of MIME content type folders. If you select a folder, you will see the
extensions to which the type maps in the right frame.
In Netscape Navigator, to find out what MIME content type maps to Excel files:
- In the browser window, open the Edit menu and select Preferences...
- In the Category window select Applications.
- In the Description window, select an application to display its MIME content
type.
Example:
Set objSAXLTmplt = Server.CreateObject("SoftArtisans.ExcelTemplate")
...
Set Recordset = adoConnect.Execute(sqlText)
objSAXLTmplt.Open Server.MapPath(Application("vroot") &_
"templates/simpletemplate.xls")
objSAXLTmplt.DataSource("Recordset") = Recordset
objSAXLTmplt.ContentType = "application/unknown"
objSAXLTmplt.Process "TemplateSample.xls", saProcessOpenInPlace
Top |
| DataSource |
DataSource sets a data source
to bind to template
data markers. The data source may be an ADO Recordset,
a variable, a
one dimensional array or
a two dimensional array.
A data marker is a
string beginning with "%%=" (or "%%=$" for variables and arrays), which is placed
in a cell of the template spreadsheet. Each data marker specifies
a database field, variable, or array column to insert in the spreadsheet column
containing the marker. Every data marker in the template spreadsheet
must refer to a DataSource which has been assigned in the ExcelWriter script.
You may assign many DataSources to a single spreadsheet.
DataSources should be set after all data manipulation is complete and before
calling the Process method.
DataSource takes two optional parameters:
Name
The data source name specified in the data marker
(e.g. "Orders" in %%=Orders.OrderID).
The string passed to the Name parameter
must begin with a letter.
Default value: "" (empty string).
If you wish to use binding by order,
you must leave the name parameter empty.
MaxRows
The maximum number of rows to return from a
database or 2-dimensional array.
Default value: 65536.
Assign the optional parameters Name and MaxRows, as
follows.
'--- Setting Name and MaxRows.
'--- Name must begin with a letter.
'--- This format cannot be used with binding by order
obj.DataSource("Name", 50) = MyDataSource
'--- Setting Name only.
'--- Name must begin with a letter
'--- This format cannot be used with binding by order
obj.DataSource("Name") = MyDataSource
'--- Setting MaxRows only. Use any of the following formats.
obj.DataSource (, 50)
obj.DataSource (50)
'--- If neither parameter is set, default settings will be used.
obj.DataSource = MyDataSource
Note: When the name parameter is not set, the DataSource should be referenced
by number in your data markers, according to the order of DataSource assignments. See binding by order.
DataSources with different formats and different types of data
may be used together the same script.
For example:
obj.DataSource = myVariable
obj.DataSource("Orders", 200) = myRecordset
obj.Datasource("twoD") = myArray
obj.Datasource = anotherRecordset
Note: ExcelWriter expects data in the order row, column. Therefore
all 2-dimensional arrays must be created to take rows for the first dimension
and columns for the second dimension.
For example, if you want to create an array in VBScript with 4 rows and 2 columns,
you should declare it as follows:
dim myArray(3,1)
'--- VBScript arrays are 0-based
'--- Alternatively you can set the lower and upper bound:
'dim myArray(1 to 4, 1 to 2)
| Data marker formats |
| Recordset data marker | %%=[DataSourceNameOrNumber.]FieldNameOrNumber
|
| Variable data marker | %%=$DataSourceNameOrNumber |
| One-Dimensional Array data marker | %%=$DataSourceNameOrNumber |
| Two-dimensional Array data marker | %%=$DataSourceNameOrNumber.#ColumnNumber
|
Important:
- In ExcelWriter version 3.1 and higher, when binding a DataSource
by position, if the number of fields
in a particular recordset is not known in advance, you may include
excess datamarkers in the template, and ExcelWriter will remove any that are not
used.
-
DataSource order numbers, and field position numbers, are 1-based.
If ExcelWriter encounters
%%=#0[.field] or %%=[DataSource.]#0
in a data marker,
an error will occur. Note: In
ExcelWriter versions before 3.1,
DataSource and field numbers were 0-based.
If you upgraded from an earlier version,
you may need to modify your code.
|
| DataSource Formats for Binding to Recordset |
| | Format | Data Marker | Code |
| 1 |
Generic |
%%=DataSourceName.FieldName |
obj.Datasource("DataSourceName") = MyDataSource |
| 2 |
Implicit bind |
%%=FieldName
Note: If there are many datasources assigned, implicit bind
assumes the first.
|
obj.Datasource = MyDataSource |
| 3 |
Bind by order |
%%=#1.FieldName
%%=#2.FieldName
|
obj.Datasource = MyDataSource
obj.Datasource = MyDataSource1 |
| 4 |
Bind by field position |
%%=DataSourceName.#1
%%=DataSourceName.#2
|
obj.Datasource("DataSourceName") = MyDataSource |
| 2 & 3 |
Implicit Bind
with
Bind by order
|
%%=FieldName
%%=#2.FieldName
|
obj.Datasource = MyDataSource
obj.Datasource = MyDataSource1
|
1,2,3 & 4 |
Generic, Implicit, Bind by order, and
Bind by field position |
%%=#1
%%=#2.#2
|
obj.Datasource = MyDataSource
obj.Datasource = MyDataSource1 |
| Note: In ExcelWriter version 3.1 and higher,
DataSource sequential numbers are 1-based; when
binding by order the first data marker is
%%=#1[.field].
If ExcelWriter encounters a %%=#0[.field]
data marker, an error will occur.
Note: In ExcelWriter version 3.1 and higher, Field position sequential numbers are
1-based. If ExcelWriter encounters a data marker
containing field 0 (e.g. %%=DataSource.#0), an
error will occur.
Note:In ExcelWriter version 3.1 and higher, when binding a DataSource
by position, if the number of fields
in a particular recordset is not known in advance, you may include
excess datamarkers in the template, and ExcelWriter will remove any that are not
used.
|
| DataSource formats for binding to Variables |
| | Format | Data Marker | Code |
| 1 |
Generic |
%%=$DataSourceName |
obj.Datasource("DataSourceName") = MyVar |
| 2 |
Bind by order |
%%=$#1
%%=$#2
|
obj.Datasource = MyVar
obj.Datasource = MyVar1 |
| 1 & 2 |
Generic Bind with Bind by order |
%%=$DataSourceName
%%=$#2 |
obj.Datasource("DataSourceName") = MyVar1
obj.Datasource = MyVar2 |
Note: As part of the bind to variable functionality,
it is possible to directly assign a string to a datasource.
ExcelWriter will automatically convert it to a variant.
For example:
obj.Datasource("DataSourceName") = "hello world"
|
| DataSource formats for binding to One-Dimensional Arrays |
| | Format | Data Marker | Code |
| 1 |
Generic |
%%=$DataSourceName |
obj.Datasource("DataSourceName") = MyArray |
| 2 |
Bind by order |
%%=$#1
%%=$#2
|
obj.Datasource = MyArray1
obj.Datasource = MyArray2 |
| 1 & 2 |
Generic Bind with Bind by order |
%%=$DataSourceName
%%=$#2 |
obj.Datasource("DataSourceName") = MyArray1
obj.Datasource = MyArray2 |
| Note: The data from a one-dimensional array
gets loaded into rows beginning at the cell with the corresponding data marker,
i.e. for each element of the array, a new row is inserted.
If you wish to accomplish the equivalent of displaying a one-dimensional array
horizontally across columns, you should create a two dimentional array with
only one column and place datamarkers horizontally.
|
| DataSource formats for binding to Two-Dimensional Arrays |
| | Format | Data Marker | Code |
| 1 |
Generic |
%%=$DataSourceName.#1 |
Dim TwoDArray(5,10) obj.Datasource("DataSourceName") = TwoDArray |
| 2 |
Bind by order |
%%=$#1.#1
%%=$#2.#1
|
Dim TwoDArray1(5,10)
obj.Datasource = TwoDArray1
Dim TwoDArray2(12,3)
obj.Datasource = TwoDArray2
|
| 1 & 2 |
Generic Bind with Bind by order |
%%=$DataSourceName.#1
%%=$#2.#1
|
Dim TwoDArray1(5,10)
obj.Datasource("DataSourceName") = TwoDArray1
Dim TwoDArray2(12,3)
obj.Datasource = TwoDArray2
|
Note: In ExcelWriter version 3.1 and higher, DataSource order and column position numbers
are 1-based.
If ExcelWriter encounters data markers with %%=#0.#columnNumber or %%=DataSource.#0,
an error will occur. Column position numbers
in the data markers must begin with #1, even if the lower bound of the array is other
than 1, as in:
Dim TwoDArray(1 to 5, 40 to 45)
Note: ExcelWriter imports data in the order row, column.
Therefore, two-dimensional arrays must be created with rows
for the first dimension and columns for the second dimension. For example:
Dim myArray(5,10) will fill 6 rows, 11 columns (VBScript
arrays are 0-based by default).
Note: Array columns must
always be bound by position, because unlike recordsets, they
do not have field names:
%%=$MyArray.#1
%%$#3.#2
Note: In ExcelWriter version 3.1 and higher, if the number of columns
in a particular 2-D array is not known in advance, one may put as many datamarkers as desired
in the template spreadsheet, and ExcelWriter will remove any that are not used.
|
Top |
| Open |
Opens an ExcelWriter template* spreadsheet, that is, a file created in
Microsoft Excel that contains data markers where values will be inserted.
A data marker is a
cell value beginning with "%%=" that specifies
a database column, variable, or array to insert in the spreadsheet column
containing the marker (see DataSource).
| * |
In the ExcelWriter documentation, the term "template" refers to an ExcelWriter
template, not a Microsoft Excel template (.xlt). However, ExcelWriter can
open and generate both .xls and .xlt files. Use an .xlt file the same way you would an
.xls file. |
The Open method takes one parameter, the file path and name of the template
.xls file to open.
Example:
objTemplate.Open "c:\templates\template.xls"
Note: The ExcelTemplate object supports Excel's BIFF8 (Excel 97/2000/XP) format
only; ExcelTemplate does not support BIFF7 (Excel 95) format templates, and
will not generate BIFF7 format files.
Top |
| PreserveStrings |
When an ExcelWriter template data marker is populated
with numeric strings, ExcelTemplate will automatically convert the strings to numbers. To
preserve a numeric string as a string, set PreserveStrings to True.
PreserveStrings is set to False by default.
Example:
objTemplate.PreserveStrings = True
Top |
| Process |
Enters database values in the template and
generates a new spreadsheet. The Process method takes three parameters:
FileName |
Path and file name of the new spreadsheet |
|
ProcessMethod |
Optional. Specifies whether to save the file to disk,
open it in Excel, open it in the browser, or
return an ExcelApplication
object |
Default value: 0 (saProcessDefault) |
ExcludeMacro |
Optional. If set to True, macros in the template will
be excluded from the generated spreadsheet.
When ExcludeMacro is enabled, buttons
associated with macros may produce the error
"Data may have been lost." Therefore, when
excluding macros,
remove all associated buttons from the
spreadsheet. |
Default value: False |
Assign the ProcessMethod parameter by name or number:
| 0 | saProcessDefault | Save the file to disk |
| 1 | saProcessOpenInExcel | Open the file in Excel |
| 2 | saProcessOpenInPlace | Open in the browser |
| 3 | saProcessOpenForScripting | Return the file in
memory as an
ExcelApplication
object. Note: This value is not available in
ExcelWriterSE, ExcelWriterLE, or ExcelWriterFree. |
Example:
objTemplate.Process "file.xls", saProcessOpenInPlace
Top
|
| SpreadsheetsCreatedToday V4 |
Returns the number of spreadsheets ExcelWriter created so far, in
a 24 hour period. Note that SpreadsheetsCreatedToday contains the
number of spreadsheets created by both ExcelApplication
and ExcelTemplate.
The hour count begins when the first spreadsheet is created. The count is reset when the first
spreadsheet is created after the previous 24 hour period. For example, if you create your first spreadsheet
at 10:00 AM on June 2, the 24 hour period will end at 10:00 AM on June 3, and the next period will begin when you
create the first spreadsheet after 10:00 AM on June 3.
Top
|
| UnicodeToAnsi V4 |
To get a non-Latin alphabet string from a spreadsheet, and
display the string correctly in the browser from a server
running a language different from the language of the client's HTML page,
you must:
- Set the ExcelWriter script's charset property to the string language's
charset.
- Use the
UnicodeToAnsi method to convert the string to
an Ansi encoded string that can be displayed correctly to the user.
UnicodeToAnsi takes a Unicode string and the string language's
code page and returns an Ansi string:
AnsiString = UnicodeToAnsi(UnicodeString, CodePage)
To correctly display strings in only one language, uses a specific language code page.
To correctly display strings in any language, use
code page 65001. For a complete list of charset and code page values see,
Character Set Recognition.
Top |
Copyright © 2003, SoftArtisans, Inc.
|
|