This article demonstrates how SACell.Format and helper methods can be used with ExcelApplication in COM/ASP to provide functionality similar to ApplyStyle.
Creating ApplyStyle Behavior
The first step in building behavior with the ExcelApplication COM object that is similar to that of the .NET ExcelApplication's ApplyStyle methods is to create a helper class.
This helper class will need three key pieces of information:
- A cell or range of cells on which to apply a style
This is what the style will be applied to.
- A style containing the settings that we want to apply
We only need to set the properties that we plan on using. (The others will be ignored.)
- A list of properties we want to apply
This is necessary because there is no way to tell which properties on the style were changed programmatically.
Once we have this information, we will use SACell.Format to set individual properties based on the settings of the style and list of properties to be applied.
We will not be able to duplicate every property of a style, however, because Cell.Format only supports a subset of style properties: Hidden, HorizontalAlignment, Locked, Number, Orientation, VerticalAlignment, WrapText, Font.Bold, Font.Charset, Font.Color, Font.Italic, Font.Name, Font.Size, Font.Strikethrough, Font.Subscript, Font.Superscript, Font.Underline
We create two subs for providing this ApplyStyle behavior, and place them in an include file called ApplyStyle.inc, so that they can easily be used in any ASP page:
Sub ApplyStyleToRange(range, refStyle, propsToCopy)
This method loops through all the cells in a range and calls ApplyStyleToCell on each cell.
Sub ApplyStyleToCell(cell, refStyle, propsToCopy)
This method loops through all the properties in the propsToCopy string and set's the specified cell's Format properties with the same values as the provided refStyle's properties.
Both methods accept the following parameters, representing the information we needed as specified above:
- range (SARange object) or cell (SACell object) on which to apply the style
- refStyle (SAStyle object) containing the actual settings to apply
- propsToCopy (String) containing comma-delimited property names that correspond to SACell.Format properties. For example:
"HorizontalAlignment,Font.Bold"
Using ApplyStyle.inc
To use ApplyStyle.inc in an ASP page with ExcelWriter, use an include statement toward the top of the page:
<!--#include file="ApplyStyle.inc"-->
Then, call either of the ApplyStyle methods from your own code.
Example
The following example demonstrates opening an Excel workbook and applying properties of a style to a range in that workbook. We create an ASP page called ApplyStyleSample.asp to contain this logic.
First, we import the TypeLib for ExcelWriter, set the script language, and include the ApplyStyle.inc file.
<!--METADATA TYPE="TypeLib" UUID="{7BCD2133-64A0-4770-843C-090637114583}" -->
<%@ Language="VBScript" %>
<% '--- Include ApplyStyle helper methods %>
<!--#include file="ApplyStyle.inc"-->
Then, we define all the objects we will need, create an instance of ExcelWriter, and open the workbook to be modified.
<%
Dim xla, cells, myRange, myStyle, myFont, myProps
'--- Instantiate ExcelWriter
Set xla = Server.CreateObject("SoftArtisans.ExcelWriter")
'--- Open workbook
xla.Open(Server.MapPath("in.xls"))
Next, we create the three objects needed to call ApplyStyleToRange: a range of cells, a style with the properties we wish to apply set appropriately, and a comma-delimited string of properties we want applied. This example has all properties included.
'--- Get cells from first worksheet
Set cells = xla.Worksheets(1).Cells
'--- Get range
Set myRange = cells.Range(2, 1, 4, 4)
'--- Create style, setting any or all properties
Set myStyle = xla.CreateStyle()
myStyle.Hidden = True
myStyle.HorizontalAlignment = sahaRight
myStyle.Locked = False
myStyle.Number = "@"
myStyle.Orientation = saoriClockwise
myStyle.VerticalAlignment = savaCenter
myStyle.WrapText = True
myStyle.Font.Bold = True
myStyle.Font.Charset = 1 '--- Changing this can affect other properties such as
'--- Font Name
myStyle.Font.Color = 255
myStyle.Font.Italic = True
myStyle.Font.Name = "Tahoma"
myStyle.Font.Size = 16
myStyle.Font.Strikethrough = True
myStyle.Font.Subscript = True '--- Setting Subscript to True makes
'--- Superscript False.
myStyle.Font.Superscript = False '--- Setting Superscript to True makes
'--- Subscript False.
myStyle.Font.Underline = saxlSingleUnderline
'--- Create comma-delimited string of properties to apply
'--- (this example has all properties included)
myProps = "Hidden,HorizontalAlignment,Locked,Number,Orientation," & _
"VerticalAlignment,WrapText,Font.Bold,Font.Charset,Font.Color," & _
"Font.Italic,Font.Name,Font.Size,Font.Strikethrough,Font.Subscript," & _
"Font.Superscript,Font.Underline"
Then, we call ApplyStyleToRange.
'--- Apply style to range
ApplyStyleToRange myRange, myStyle, myProps
Finally, we save the workbook and stream it to the user.
'--- Stream workbook to user
xla.Save "out.xls", saOpenInExcel, saFileFormatDefault
%>
This full example is included as an attachment to this article. It contains:
- ApplyStyle.inc - ApplyStyle helper methods
- ApplyStyleSample.asp - Sample page that uses ApplyStyleToRange
- in.xls - Starting Excel workbook
|