Example: Creating Your First SpreadsheetFirst.asp creates a simple spreadsheet, assigns cell values, and opens the new spreadsheet in the browser. Note that first.asp does not contain HTML. This is because the generated spreadsheet is written (to disk, memory, the browser, or to MS Excel) in a single ASP response. If the response includes HTML, the spreadsheet will be corrupted. The ASP script should contain only the creation of the spreadsheet. Do not include HTML or Response.Write lines. To create a spreadsheet, the first step is to create an instance of the ExcelWriter object:
Set xlw = Server.CreateObject("SoftArtisans.ExcelWriter")
Next, create a Worksheet: Set ws = xlw.Worksheets(1) A spreadsheet may contain multiple worksheets.
The first worksheet in a spreadsheet is Within the new worksheet, create cells and fill in values:
ws.Cells("A1").Value = "Name"
ws.Cells("B1").Value = "Count"
ws.Cells("C1").Value = "Dollar"
ws.Cells("A2").Value = "Fred Smith"
ws.Cells("B2").Value = 10
ws.Cells("C2").Value = 37.5When the spreadsheet is complete, you can save it to the server's hard disk, return the file in memory, or open the file in either the browser or Microsoft Excel. In this example, ExcelWriter opens the spreadsheet in the browser: xlw.Save "first.xls", saOpenInPlace The spreadsheet has been created in memory and is now sent to the user's browser. The user may choose to save the spreadsheet. If it is saved, the default name will be first.xls. Avoiding Errors by Using TypelibsIn ASP, Typelibs provide quick and convenient access to constants associated with a particular object. The script in this example includes ExcelWriter's TypeLib because the script uses the constant saOpenInPlace. Avoid errors by always including the TypeLib in your ExcelWriter scripts. The UUID attribute specifies ExcelWriter's unique identifier:
<!--METADATA TYPE="TypeLib" UUID="{7BCD2133-64A0-4770-843C-090637114583}" -->
Wrapping UpFinally, the ExcelWriter object is explicitly destroyed. This is not strictly necessary but is considered good programming practice. The ASP Response is also ended. This prevents extraneous additional characters from being sent along with the spreadsheet. Set xlw = nothing Response.end Section Summary
Copyright © 2003, SoftArtisans, Inc. |