xlsgen > overview > VB, VBA developer

If you are a VB or VBA developer then this section will get you up and running. The remainder of this section assumes that xlsgen is already installed on your machine.

In your programming environment, click on Tools \ References as in the capture below : (please note the name of the menu options vary according to the language of your programming environment. The following assumes that an English version is being used.)


When the References dialog box, check out whether the component titled ExcelGenerator. ARsT design. is already listed and checked. If yes, then click on OK and you can skip what's below.

Click on the Browse button on the right, then browse the folders for the following file : c: \ program files \ xlsgen \ xlsgen.dll.


The component should appear, selected and checked as in the screen capture above. If the component does not show, it is either not installed, or not registered. Reinstall the software and pay attention to the message boxes that show up, if any. Please contact the technical support.

Click OK. Your project is ready to use xlsgen.

Objects and underlying programming interfaces can be looked up by bringing the object browser (hit F2). A screen capture like that one should appear :


 

Generating an Excel file always begin with a prologue, and always ends with an epilogue.

The prologue can be broken down as follows :

And this can be done with the following code :

VB code

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

Dim wbk As IXlsWorkbook
Set wbk = engine.New("c:\myfile.xls")

Dim wksht As IXlsWorksheet
Set wksht = wbk.AddWorksheet("sheet1")

... ' your code begins here

Then programming interfaces can be used to put content in cells, and add all the necessary code to generate the excel file.

Until you end up with the epilogue, which can be broken down as follows :

And this can be done with the following code :

VB code

... ' your code ends here

wbk.Close

Set engine = Nothing

All in all, the entire code pattern is :

VB code

Sub mycustomcode()

  ' create a running instance of the generator
  Dim engine As CoXlsEngine
  Set engine = CreateObject("ExcelGenerator.ARsTDesign")

  ' create a new Excel file, and get a workbook to work with
  Dim wbk As IXlsWorkbook
  Set wbk = engine.New("c:\myfile.xls")

  ' create a new worksheet
  Dim wksht As IXlsWorksheet
  Set wksht = wbk.AddWorksheet("sheet1")

  ... ' your code begins here

  ' commit all the work, and make sure the .xls file is ready to use
  wbk.Close

  ' free the memory from the running instance
  Set engine = Nothing

End Sub

 

Please note that as you type VB code, intellisense provides you with all interfaces and objects involved, as in :



 

ASP (VBScript, JScript) developer

ASP specific information

If xlsgen is used to generate and write Excel files on the file system (very likely), it must be ensured that the target folder has effectively been granted write permissions. Remember that when running a web application, the user is not the local interactive account, but ASP/ASPNET (or NETWORKSERVICE if you are running on Windows Server 2003) and those users are not granted such right for obvious reasons.

Make sure to know that while there is no restriction or significant drawback to writing Excel files on the file system in general, xlsgen actually supports an in-memory scenario which does not require write permissions to be granted and is thus perhaps better suited to meet restricted environments. The xlsgen install comes with a code sample called 'inmemory' showing how to proceed. To grant write permissions to an ASP/ASP.NET web server :

  • create a folder, let's call it C:\tmp
  • right-click and select Properties
  • click on the Web Sharing tab
  • click on Share this folder
  • Provide a name alias (that does not really matter)
  • Make sure to check the Write permission before you click ok

If you do that, you are all set (no need to restart IIS). For instance, you can write a statement like xlsgenEngine.New(@"c:\tmp\test.xls"); and forget about access-denied errors.

Note that you can do this as well from the IIS control panel. Just right-click there, create a virtual folder, and then proceed with the name alias, Write permission and actual file system folder.

If you are a VBScript or JScript developer then this section will get you up and running. The remainder of this section assumes that xlsgen is already installed on the server-side.

An equivalent technique than for VB, VBA developers is required to start a session with ASP code. Below is an example of code pattern :

ASP VBScript code
<%@ Language="VBSCRIPT" %>

<%

Dim szOutputFolder
szOutputFolder = "c:\Inetpub\excelfiles\"

' create a server-side instance of the generator
If IsEmpty(Session("MyObj")) Then
	Set Session("MyObj") = Server.CreateObject("ExcelGenerator.ARsTdesign")
End If

If Not(IsEmpty(Session("MyObj"))) Then

  ' create a new Excel file, and retrieve a workbook
  Dim wbk
  Set wbk = Session("MyObj").New(szOutputFolder & "myfile.xls")

  ' create a new worksheet
  Dim wksht
  Set wksht = wbk.AddWorksheet("sheet1")

  ' sample code
  wksht.Label(9,1) = "hello world!"
  ' your code begins here
  ' ...

  wbk.Close

  ' notify IIS of the actual Excel output
  Response.Redirect szOutputFolder & "myfile.xls"

End If

%>

Since this code runs the generator on the server-side, the client machine need not xlsgen installed at all.

If you allow your web server to retrieve arbitrary buffer content from a physical file, then instead of redirecting the output to the .xls file above, you could just as well directly output the buffer along with the Excel MIME type. The destination machine at the other end needs to have Excel installed in order to see it automatically open. Code goes like this :

ASP VBScript code, server-side code

Response.Buffer = true
Response.CacheControl = "no-cache"

' ... paste all the same than above ...

' grab the raw binary content from myfile.xls
' TODO : buffer = ... ( szOutputFolder & "myfile.xls" )

' .XLS  ==> application/vnd.ms-excel
' .XLSX ==> application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
' .XLSM ==> application/vnd.ms-excel.sheet.macroEnabled.12
' .XLTX ==> application/vnd.openxmlformats-officedocument.spreadsheetml.template
' .XLTM ==> application/vnd.ms-excel.template.macroEnabled.12
' .XLAM ==> application/vnd.ms-excel.addin.macroEnabled.12
Response.ContentType = "application/vnd.ms-excel"
Response.BinaryWrite ( buffer )
Response.End

Please note that with scripting code, objects used are never typed, that's why the underlying programming interfaces never appear explicitely in the code.

Below is the equivalent code for JScript developers :

ASP JScript code, server-side code
<%@ Language="JSCRIPT" %>

<%

  var szOutputFolder
  szOutputFolder = "c:\\Inetpub\\excelfiles\\"

  // create a server-side instance of the generator
  var xlsgen = Server.CreateObject("ExcelGenerator.ARsTdesign")

  // create a new Excel file, and retrieve a workbook

  var wbk = xlsgen.New(szOutputFolder + "myfile.xls")

  // create a new worksheet
  var wksht = wbk.AddWorksheet("sheet1")

  // sample code
  wksht.Label(9,1) = "hello world!"
  // your code begins here
  // ...

  wbk.Close()

  Response.Redirect (szOutputFolder + "myfile.xls")

%>

There are ASP.NET code samples written in C# available in the install.

Instead of executing code on the server-side, you can, depending on the requirements, have the generator execute on the client-side. Of course, this requires xlsgen to be installed on the client-side.

Below is the VBScript and JScript code for client-side. Typical scenarios are local script execution.

VBScript code, client-side code

  ' xlsgen_clientside.vbs
  ' to run this code, double-click on the .vbs file

  Dim szOutputFolder
  szOutputFolder = "c:\excelfiles\"

  Dim xlsgen
  Set xlsgen = CreateObject("ExcelGenerator.ARsTdesign")
  
  ' create a new Excel file, and retrieve a workbook
  Dim wbk
  Set wbk = xlsgen.New(szOutputFolder & "myfile.xls")

  ' create a new worksheet
  Dim wksht
  Set wksht = wbk.AddWorksheet("sheet1")

  ' sample code
  wksht.Label(9,1) = "hello world!"
  ' your code begins here
  ' ...

  wbk.Close

  ' free the memory
  Set xlsgen = Nothing

JScript code, client-side code

  ' xlsgen_clientside.js
  ' to run this code, double-click on the .js file
  
  var szOutputFolder
  szOutputFolder = "c:\\excelfiles\\"

  // create a server-side instance of the generator
  var xlsgen = new ActiveXObject("ExcelGenerator.ARsTdesign")

  // create a new Excel file, and retrieve a workbook

  var wbk = xlsgen.New(szOutputFolder + "myfile.xls")

  // create a new worksheet
  var wksht = wbk.AddWorksheet("sheet1")

  // sample code
  wksht.Label(9,1) = "hello world!"
  // your code begins here
  // ...

  wbk.Close()

  // free the memory
  xlsgen = null

 

C#, Managed C++, ASP.NET developer

There are ASP.NET code samples written in C# available in the install.

An equivalent technique than for VB, VBA developers is required to start a session with .NET code. This session is described in this product tour.

The resulting code pattern for an arbitration Excel generation is the following :

C# code

// create a running instance of the generator
xlsgen.CoXlsEngine engine = new xlsgen.CoXlsEngine();

// create a new Excel file, and retrieve a workbook to work with
IXlsWorkbook wbk = engine.New( @"c:\myfile.xls" );

// create a new worksheet
IXlsWorksheet wksht = wbk.AddWorksheet( "sheet1" );

// your code begins here
...

// commit all work, and make sure the .xls file is ready to use
wbk.Close();

// free the memory from the running instance
System.Runtime.InteropServices.Marshal.ReleaseComObject(engine);

ASP.NET specific information

If xlsgen is used to generate and write Excel files on the file system (very likely), it must be ensured that the target folder has effectively been granted write permissions. Remember that when running a web application, the user is not the local interactive account, but ASP/ASPNET (or NETWORKSERVICE if you are running on Windows Server 2003) and those users are not granted such right for obvious reasons.

Make sure to know that while there is no restriction or significant drawback to writing Excel files on the file system in general, xlsgen actually supports an in-memory scenario which does not require write permissions to be granted and is thus perhaps better suited to meet restricted environments. The xlsgen install comes with a code sample called 'inmemory' showing how to proceed. To grant write permissions to an ASP/ASP.NET web server :

  • create a folder, let's call it C:\tmp
  • right-click and select Properties
  • click on the Web Sharing tab
  • click on Share this folder
  • Provide a name alias (that does not really matter)
  • Make sure to check the Write permission before you click ok

If you do that, you are all set (no need to restart IIS). For instance, you can write a statement like xlsgenEngine.New(@"c:\tmp\test.xls"); and forget about access-denied errors.

Note that you can do this as well from the IIS control panel. Just right-click there, create a virtual folder, and then proceed with the name alias, Write permission and actual file system folder.

ASP.NET 64-bit specific information

By default ASP.NET 64-bit has stronger and tightened security configuration bits. Which results in having to make changes in order to run xlsgen on a 64-bit OS using ASP.NET. It works as follows :

In order to be able to run xlsgen at all, you must use the isolated server (or use the 64-bit version of xlsgen.dll, which we manually send) since ASP.NET is a 64-bit application. See the isolated server pages for setting it up.

The execution access bits must be manually set to "everyone" using Windows's DCOMcnfg.exe tool. In order to do this, hit Windows+R, enter DCOMcnfg.exe and hit enter. Unfold "Component services / Computers / My computer / DCOM configuration", and you should see a key called "ExcelGenerator". Right-click on it, and click on the Security tab. From there, change the appropriate security option to "everyone" (loosen up the screw).

In case you don't see the "ExcelGenerator" key, it is probably because you are running Windows 2008 server R2 or Windows 7 64-bit. There is a behavior change in Windows, and the workaround works as follows. Hit Windows+R, enter mmc -32 and hit enter. Then add the Component services snap-in from there. That should fix it and from now on it will be possible to use DCOMcnfg.exe again.

.NET specific information

Due to the undeterministic nature of the .NET garbage collection mechanism, you must ensure that the xlsgen object instances are never prematurely collected, otherwise this will lead to a program execution crash. For some reason that should be explained by the implementers of the .NET run-time, the garbage collector sometimes incorrectly believes that objects being used are...not being used anymore and can be collected. This can cause xlsgen objects to be prematurely disconnected. To avoid the problem, you can "pin" the objects. Here is how.

  • if you are using managed C++, you can use the __pin keyword in front of objects
  • if you are using C#, VB.NET or any other .NET language, you can indirectly pin an object by moving where it's declared, for instance by declaring the object as a class member instead of within a local function.
  • a careful .NET citizen should release a COM component such as xlsgen by calling the following method when the component is not needed anymore : System.Runtime.InteropServices.Marshal.ReleaseComObject(engine);

 

Java developer

Java provides a great opportunity for the Java community to benefit from a versatile Excel generator. The Java code generation is not tied to any Java x.yy release so it should work with all of them.

At run-time, the Java code calls an xlsgen library (xlsgen.jar) that is a proxy to an interop layer to native code. The interop layer uses JNI (Java Native Interface) to interface with C code and actually make COM calls. Since any Java method call in the object model eventually makes a COM call, this only work on Windows.

Because of this, at run-time you need not only xlsgen.jar but also a proxy dll called xlsgen_jproxy.dll. xlsgen.jar is a regular Java package which simply reflects the xlsgen object model (a workbook can create one or more worksheets and so on). The source code for this library is given for documentation purpose only. xlsgen_jproxy.dll is a native dll with over 300 native functions that are in turn mapped to the actual COM object model. Java types are mapped into native types back and forth.

How to build a Java piece of code :

  1. First of all, install a JDK to begin with.
  2. Once a JDK is installed, either you have a JAVA_HOME environment variable created for you in your system environment variables (Control panel / System / User(Advanced) / Environment Variables), then you are ready to go. Otherwise, go in your environment variables, create a new variable called JAVA_HOME, and make it point to the actual JDK subfolder, where executable files javac.exe and java.exe can be found. Note that using a variable like JAVA_HOME is pure convenience, not a requirement.
  3. The following assumes you are building a Java project using a command-line. Of course IDEs like IntelliJ, Eclipse and NetBeans let you do things somewhat differently. Open up a command-line window.
  4. Type set PATH=%JAVA_HOME%;%PATH%. This makes sure the compiler can be accessed from the command-line.
  5. Go in a Java code sample folder
  6. Type javac -classpath .;..\xlsgen.jar test.java, where test.java is the name of a Java file to compile and xlsgen.jar is the library available from the parent folder.
  7. This should get your code compiled. File test.class should be created/updated.

How to run a Java code :

  1. Build the code using the previous steps
  2. Make sure that file xlsgen_jproxy.dll exists, and is in the same folder than test.class. Why the same folder? In fact, the proxy gets loaded in memory the moment an instance of the XlsEngine class gets created. Which is exactly what test.class does.
  3. Make sure that xlsgen.jar is still available and in the parent folder
  4. Open up a command-line and type java -classpath .;..\xlsgen.jar test
  5. The code should run.

Note, file xlsgen.jar can be placed just about anywhere. The steps above simply assume this file is in the parent folder for demonstration purpose. xlsgen.jar was compiled using JDK 1.4.2. You can always rebuild xlsgen.jar using a different JDK version by just following the steps in readme.txt file in the java/xlsgen folder of the install.

Here is a sample Java code that creates a trivial Excel workbook :

Java code

import xlsgen.*;

class test {

    public static void main(String[] args) {

        // create an engine instance
        XlsEngine engine = new XlsEngine("./../../../xlsgen.dll");

        // create a new Excel file
        XlsWorkbook workbook = engine.New("myfile.xls");

        // create a worksheet
        XlsWorksheet wksht = workbook.AddWorksheet("sheet1");

        // put a label
        wksht.putLabel(1 /*row*/,1 /*column*/, "Hello!");

        // we are done
        workbook.Close();

    }
}

 

VB.NET developer

VB.NET has a slightly different syntax than C#. You have to use the Imports statement instead of using. Forget about prefixing assignments with a Set keyword. Create an Excel workbook with a CoXlsEngine object. And whenever you create a new Excel workbook, you must enclose the New statement in closed brackets, because the VB.NET compiler is confused with the New symbol otherwise used for constructors.

In short, here is a regular VB.NET sample code :

VB.NET code

  ' the statement that allows us to use the xlsgen object model from VB.NET code
  Imports Interop.xlsgen

  ' create a running instance of the generator
  Dim engine As New CoXlsEngine

  ' create a new Excel file, and retrieve a workbook to work with
  Dim wbk As IXlsWorkbook
  wbk = engine.[New]("c:\myfile.xls")

  ' create a new worksheet
  Dim wksht As IXlsWorksheet
  wksht = wbk.AddWorksheet("sheet1")

  ... ' your code begins here

  ' commit all the work, and make sure the .xls file is ready to use
  wbk.Close

  ' free the memory from the running instance
  engine = Nothing

 

AutoIt developer

The AutoIt scripting language is very much like VBScript, supports COM/Automation and can use xlsgen.

AutoIt code

Local $engine = ObjCreate("ExcelGenerator.ARsTdesign")

$workbook = $engine.New("sample.xlsx")

$worksheet = $workbook.AddWorksheet("Sheet1")

$worksheet.Label(2, 3) = "Hello world!"

$workbook.Close

$engine = 0

Exit

 

PowerShell developer

 

PowerShell scripting code

$engine = new-object -ComObject ExcelGenerator.ARsTDesign
$workbook = $engine.New( "C:\tmp\myfile.xlsx" )

$wksht001 = $workbook.AddWorksheet( "Sheet1" )

$wksht001.Cell(1,2).HtmlFloat = "<font color=#000000 size=11 name=""Calibri"">154</font>"
$wksht001.Cell(1,3).HtmlLabel = "<font color=#000000 size=11 name=""Calibri"">this is a test</font>"
$wksht001.Cell(3,3).HtmlFloat = "<font color=#000000 size=11 name=""Calibri"">1</font>"
$wksht001.Cell(4,3).HtmlFloat = "<font color=#000000 size=11 name=""Calibri"">2</font>"

$workbook.Close()

 

C/C++ developer

There are samples part of the install written in C and C++.

C/C++ provides the most granular way to access COM with two techniques, depending on which you prefer :

Choice 1 involves a technique known as smart pointers.

Choice 2 involves the use of raw COM interfaces.


1) using smart pointers

In order to use smart pointers, the type-library embedded in the Excel generator must be imported. This is done with a single statement in your code :

C/C++ code
#import "c:\program files\xlsgen\xlsgen.dll" no_function_mapping

//alternatively, depending on your Visual Studio C++ compiler version,
//
//#import "progid:ExcelGenerator.ARsTdesign" no_function_mapping
//
//or
//
//#import "libid:2BDEA919-2489-4ED2-8F55-C12D74DAD9B8" no_function_mapping

If you run your project at this point, two new files will get automatically generated. The xlsgen.tlh and xlsgen.tli files are wrappers around the xlsgen programming interfaces and hide a lot of technical details behind COM programming.

The code pattern resulting from the use of smart pointers is :

C/C++ code

// initialize the COM library
::CoInitialize(NULL);

{ // braces are here to reduce the scope of the smart pointers (automatic gc)

  // create a running instance of the generator
  xlsgen::IXlsEnginePtr engine( __uuidof(xlsgen::CoXlsEngine) );

  // create a new Excel file, and retrieve a workbook to work with
  xlsgen::IXlsWorkbookPtr wbk;
  wbk = engine->New( L"myfile.xls" );

  // create a new worksheet
  xlsgen::IXlsWorksheetPtr wksht;
  wksht = wbk->AddWorksheet( L"sheet1" );

  // your code begins here
  // ...

  // commit all work, and make sure the .xls file is ready to use
  wbk->Close();

} // the end of scope automatically frees the memory from all out-of-scope objects
// in case you are not using variable scoping, make sure to call 
// the .Release() method on each smart pointer, as in :
//  wksht.Release();
//  wbk.Release();
//  engine.Release();

// frees the COM library
::CoUninitialize();

If for any reason, you want to see the generated file in Excel, then use the following code :

C/C++ code
::ShellExecute(NULL,"open","myfile.xls",NULL,NULL,SW_SHOW);

  2) using raw COM interfaces

If you don't like C++ smart pointers, you may want to use the raw COM interfaces. Either case, it performs COM early binding so the performance should not change.

In order to make that happen, the C/C++ developer needs to program against the resulting interface headers. Those headers are provided with the setup, check out the xlsgen_i.h and xlsgen_i.c files. When xlsgen is installed, those files are installed along in that directory : c: \ program files \ xlsgen. If you want to create those headers by hand, then the procedure is as follows :

The xlsgen_i.h file holds all interface declarations. The xlsgen_i.c file holds the GUIDs of objects and interfaces.

In your project, add the following lines of code :

C/C++ code
#include <objbase.h>
#include "xlsgen_i.c" // relative or absolute path might be required for compilation
#include "xlsgen_i.h" // relative or absolute path might be required for compilation

The code pattern resulting from the use of raw COM interfaces is :

C/C++ code

// initialize the COM library
::CoInitialize(NULL);
{
  // create a running instance of the generator
  IXlsEngine* pGenerator = NULL;
  HRESULT hr = ::CoCreateInstance(CLSID_CoXlsEngine, 
                                  NULL, CLSCTX_ALL , 
                                  IID_IXlsEngine, 
                                  (void**)&pGenerator);
  if (SUCCEEDED(hr))
  {
    // create a new Excel file, and retrieve a workbook to work with
    IXlsWorkbook* pWbk= NULL;
    pGenerator->New( L"myfile.xls", &pWbk);

    // create a new worksheet
    IXlsWorksheet* pWksht = NULL;
    pWbk->AddWorksheet( L"sheet1", &pWksht);

    // your code begins here
    // ...

    // commit all work, and make sure the .xls file is ready to use
    pWbk->Close();

    // free the memory from the running instance
    pWksht->Release();
    pWbk->Release();
    pGenerator->Release();
  }
}
::CoUninitialize();

If for any reason, you want to see the generated file in Excel, then use the following code :

C/C++ code
::ShellExecute(NULL,"open","myfile.xls",NULL,NULL,SW_SHOW);

 

xlsgen documentation. © ARsT Design all rights reserved.