Introduction
Aspose.Cells is a .NET component so it works ideally when called from managed code. But many people still need to use classical ASP or any other COM clients therefore they have to instantiate Aspose.Cells classes and use their methods within such clients.
The concepts of interoperability between managed and unmanaged code, marshalling, CCW and RCW wrappers are beyond the scope of this topic. If you want to get more details about these concepts, please refer to the following articles of MSDN library:
Interoperating with unmanaged code - in general
Exposing .NET Framework components to COM
Aspose.Cells was not initially designed with COM clients in mind so you might experience some problems during integration. Please report the problems in the support forums and we will try to help asap.
Requirements
You need to have .NET Framework 1.0 or greater installed if you want to use Aspose.Cells.
Installation
You should use the full installer Aspose.Cells.msi because it registers Aspose.Cells.dll for COM interoperability automatically. If you just copy a hotfix Aspose.Cells.dll it will not work as COM registration is tied to the dll version.
If you still want to apply a hotfix you need to register it manually by executing this command:
|
regasm Aspose.Cells.dll /codebase
|
regasm.exe is a tool included in .NET Framework and must be somewhere in the C:\WINNT\Microsoft.NET folder.
ProgID
There is only one publicly creatable COM object with ProgID: Aspose.Cells.Workbook.
Usage
Please note that Aspose.Cells does not implement COM interfaces explicitly and relies on .NET ability to generate class interfaces automatically. This allows to use .NET classes reliably from within scripting clients (ASP, VBScript) that use late binding, but it is not recommended to use from within application that use early binding to a virtual table as it can create versioning problems. There is no type library provided with the component.
Instantiating of a .NET object inside a COM client is similar to creating a COM object:
Example:
Dim Excel
Set Excel = CreateObject("Aspose.Cells.Workbook")
After instantiating, you are able to access the object methods and properties as it was a COM object:
Example:
Excel.Open("book1.xls")
There are two important points you should keep in mind: Overloaded methods & Usage of enumerations.
- Some methods have overloads and they will be exposed to COM clients with a number suffix added to them except the very first method that stays unchanged. For example, Workbook.Save method overloads become Workbook.Save, Workbook.Save_2, Workbook.Save_3, and so on. The order in which method overloads are shown in the API documentation corresponds to the way they are numbered. This is the approach that .NET COM Interop takes to make overloaded methods available to COM clients.
- Enumeration values like FileFormatType.Excel2000 - you will need to generate a type library from Aspose.Cells.dll and open it in some type library viewer to lookup the enumeration values. You can view a type library using Microsoft Visual Basic 6, Visual Basic for Applications in Microsoft Word or OLE Viewer. To generate a type library, use another .NET Framework SDK tool, tlbexp:
Classical ASP example
This example demonstrates how to use Aspose.Cells from a classical ASP page to dynamically generate a document.
<%@ LANGUAGE = VBScript %>
<% Option Explicit %>
<html>
<head>
<title>Aspose.Cells classical ASP sample</title>
</head>
<body>
<h3>Aspose.Cells classical ASP sample</h3>
<form name=Form1 method=Post action="sample.asp">
<p>Please enter your name below (both first and last):<p>
<p>First name: <input type=Text name=fname></p>
<p>Last name: <input type=Text name=lname></p>
<input type=Submit value="Generate document">
</form>
<%
If Request.form("fname") <> "" AND Request.form("lname") <> "" Then
'Create a Workbook object
Dim xls
Set xls = CreateObject("Aspose.Cells.Workbook")
'Open a designer file (template)
xls.Open_5 "c:\book1.xls"
'Put data into this file
Dim sheet
Set sheet = xls.worksheets.item(0)
Dim cells
Set cells = sheet.cells
Dim cell
Set cell = cells.item_3("A1")
cell.PutValue_5 "Hello, " & Request.form("fname") & " " & Request.Form("lname") & "!"
'Save the document to the stream
Dim stream
set stream = xls.SaveToStream()
Response.Clear
'Specify the document type
Response.ContentType = "application/vnd.ms-excel"
'Other options:
'Response.ContentType = "text/plain"
'Response.ContentType = "text/html"
'Specify how the document is sent to the browser.
Response.AddHeader "content-disposition","attachment; filename=MyBook.xls"
'Another option could be:
'Response.AddHeader "content-disposition","inline; filename=MyBook.xls";
'Get data bytes from the stream and send it to the response.
Dim bytes
bytes = stream.ToArray()
Response.BinaryWrite(bytes)
Response.End
End If
%>
</body>
</html>