xlsgen > overview > COM-free Deployment

COM-free deployment is a technique meant to avoid the requirement to register xlsgen as a COM component. Makes it easier to successfully deploy applications using xlsgen. This technique works across all Windows operating system versions.

It's optional, so if you are happy with the way xlsgen works for you, just ignore the remainder of this page, existing xlsgen code will continue to run just fine.

xlsgen does not require COM registration anymore. COM registration is a standard mechanism, and all standard Setup build tools (such as InstallShield, Wise, Install2Go, the VisualStudio Setup wizard, ...) support this mechanism. And COM registration can be done by hand at any moment using the following command-line : regsvr32 <path>\xlsgen.dll to register the component, and regsvr32 -u <path>\xlsgen.dll to unregister it.

Note : if you are using Windows 7 or Windows 8, manually registering COM components such as xlsgen requires an additional step because the user does not run with administrative privilege. You can create a register.bat file, copy the proper statement in it (i.e. regsvr32 <path>\xlsgen.dll), save it, then right-click on it and choose Run as administrator.

The COM-free deployment technique comes handy. It means it becomes possible to deploy and run this application in limited-user environments. Since the registry is not used, it means the xlsgen.dll file can simply be copied anywhere on the hard drive and be referenced by its sole path in some client application code. As a bonus, this technique works regardless the operating system version it runs on.

Avoiding COM registration is a great benefit to high-level programming languages such as Java and .NET languages. It's indeed not obvious why a Java developer writing a Java application should have to deal with COM to begin with.

xlsgen exposes an entry-point, a regular Start() function, and you can take advantage of it to directly load xlsgen, get an instance of IXlsEngine the root interface to the API, and start using it.

To C/C++ developers, it means you can forget about CoInitialize()/CoCreateInstance()/CoUninitialize() and replace it with a cold Start() function call. Here is a sample code in C++ :

C++ code

::CoInitialize(NULL);
xlsgen::IXlsEnginePtr engine( __uuidof(xlsgen::CoXlsEngine) );


// note : the xlsgenLoader class loads xlsgen as a regular dll, and calls 
//  the Start() entry point
xlsgenLoader xlsgen("xlsgen.dll");

if (!xlsgen.IsInitialized())
  return;
  
xlsgen::IXlsEnginePtr engine = xlsgen.Start();
xlsgen::IXlsWorkbookPtr wbk = engine->New( L"sample1.xls" );
xlsgen::IXlsWorksheetPtr wksht = wbk->AddWorksheet( L"samplesheet" );
wksht->Label[1][2] = L"Hello world!";
wbk->Close();

In order to deploy a C/C++ application this way, you only need to make sure file "xlsgen.dll" is accessible accordingly to the path it is being referred to in the code above. In most cases, you probably want to put a copy of file "xlsgen.dll" in your application folder (and a copy of the license file).

 

To Java developers, an XlsEngine constructor overload lets you pass the fully qualified path of the xlsgen.dll file, and that's it. You get an instance of the component in return and are ready to go. The implementation of this constructor in turn loads the dll given its path, and calls the Start() function. Here is a sample code in Java :

Java code

XlsEngine engine = new XlsEngine();

XlsEngine engine = new XlsEngine("xlsgen.dll");

XlsWorkbook workbook = engine.New("sample.xls");
XlsWorksheet wksht = workbook.AddWorksheet("samplesheet");
wksht.putLabel(1,2, "Hello world!");
workbook.Close();

In order to deploy a Java application this way, you only need to make sure file "xlsgen.dll" is accessible accordingly to the path it is being referred to in the code above. In most cases, you probably want to put a copy of file "xlsgen.dll" in your application folder (and a copy of the license file).

 

To .NET developers, the only thing to do in your code is to add a simple [DllImport("xlsgen.dll")] platfom invoke call, and then call the Start() method. It returns an IXlsEngine instance, and you are ready to go. Here is a sample code in C# :

C# code

xlsgen.CoXlsEngine engine = new xlsgen.CoXlsEngine();

[DllImport("xlsgen.dll")]
static extern IXlsEngine Start();

IXlsEngine engine = Start();
IXlsWorkbook wbk = engine.New( @"sample.xls" );
IXlsWorksheet wksht = wbk.AddWorksheet( "samplesheet" );
wksht.set_Label(1,2, "Hello world!");
wbk.Close();

In order to deploy a .NET application this way, you only need to make sure that file "interop.xlsgen.dll" is in the application folder, and file "xlsgen.dll" is accessible accordingly to the path it is being referred to in the code above. In most cases, you probably want to put a copy of file "interop.xlsgen.dll" and file "xlsgen.dll" in your application folder (and a copy of the license file). File "interop.xlsgen.dll" is produced by Visual Studio at compile-time. You can also produce this file using the .NET tlbimp tool from the .NET SDK.

 

To VB (classic VB, i.e. version 5.x/6.x or VB.NET) developers, the following pattern can be used :

VB code

Dim engine As CoXlsEngine
Set engine = CreateObject("ExcelGenerator.ARsTDesign")

' this piece of code must be added in a VB module

Declare Function Start Lib "xlsgen.dll" () As CoXlsEngine


Sub generate()

Dim engine As CoXlsEngine
Set engine = Start()

Dim wbk As IXlsWorkbook
Set wbk = engine.New("sample.xls")

Dim w As IXlsWorksheet
Set w = wbk.AddWorksheet("name1")

w.Label(1,2) = "Hello world!"

wbk.Close

End Sub

In order to deploy a VB application this way, you only need to make sure file "xlsgen.dll" is accessible accordingly to the path it is being referred to in the code above. In most cases, you probably want to put a copy of file "xlsgen.dll" in your application folder (and a copy of the license file).

If you are using VBA, not VB, using the Declare statement implies to put the code above in a module window, not in a worksheet or workbook code window : when you are in the VBA editor, right-click on the project node on the left, and choose Insert / Module.

 

To Delphi developers, the following pattern can be used :

Delphi code

type
   TStart = function(): Pointer; stdcall;

var
  Handle: THandle;
  Start: TStart;
  engine : IXlsEngine ;
  wbk : IXlsWorkbook ;
  wksht : IXlsWorksheet ;
begin
   OleCheck(CoCreateInstance(
      Class_CoXlsEngine,
      nil,
      CLSCTX_ALL,
      IXlsEngine,
      engine));
  Handle := LoadLibrary('xlsgen.dll');
  @Start := GetProcAddress(Handle, PAnsiChar('Start'));
  engine := IXlsEngine(Start());
  wbk := engine.New('sample.xls');
  wksht := wbk.AddWorksheet('Sheet1');
  wksht.Label_[2,3] := 'Hello World' ;
  wbk.Close();
  engine := Nil
end;

 

To Python developers, the following pattern can be used :

Python code


# -*- coding: UTF-8 -*-
#
#-------------------------------------------------------------------------------
# Project:     XLSGen com-free sample
# Author:      Riccardo Gusmeroli, PhD
#              rgusmero@elet.polimi.it
# Created:     04/08/2014
#-------------------------------------------------------------------------------
#
# Copyright (c) 2014, Riccardo Gusmeroli
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
# 3. Neither the name of the <organization> nor the
#    names of its contributors may be used to endorse or promote products
#    derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ''AS IS'' AND ANY
# EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL <COPYRIGHT HOLDER> BE LIABLE FOR ANY
# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
# (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
# ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


import comtypes.client
import ctypes

dllpath = 'xlsgen.dll'

try:

    tlxlsgen = comtypes.client.GetModule(dllpath)

    pDLL = ctypes.CDLL(dllpath)
    pDLL.Start.restype = ctypes.POINTER(tlxlsgen.IXlsEngine)
    engine = pDLL.Start()
    if not engine:
        raise ValueError('Error calling Start function')


except Exception as e:
    sys.exit(str(e))

print 'Working with xlsgen, Version '+engine.ComponentVersion

fname = 'sample.xls'
label = "Hello world"
row = 2
col = 1

print 'We generate file "{0}" with "{1}" written in cell ({2},{3}), right aligned'\
    .format(fname,label,row,col)

workbook = engine.New(fname)
worksheet = workbook.AddWorksheet("Sheet1")
worksheet.Label[row,col] = "Hello world"
worksheet.StyleFromLocation(row,col).Alignment.Horizontal = tlxlsgen.halign_right
workbook.Close()

 

Actually, code samples related to charts all show a way to load xlsgen without relying on COM. Go check them out.

What makes this possible? A good part of the reason why it is possible in the first place is that xlsgen.dll is a dll, not an executable. Crossing the executable boundaries would require some specific technology, arguably part of the COM library (COM implements a typical client/server socket transport to cross process boundaries). So as long as xlsgen ships as a dll, it is possible to avoid COM. Note that the COM layer still brings a great interop layer which in turn gives you intellisense in your IDE, as well as the object browser in Visual Studio/Eclipse/IntelliJ. So it's not like COM should be burnt and forgotten. But for deployment purposes and run-time, it's not a requirement anymore.

 

 

xlsgen documentation. © ARsT Design all rights reserved.