xlsgen > overview > .NET best practices

If you are writing .NET code that targets xlsgen, you don't have to worry about anything in particular. But, to maximize the performance/benefits, here follows a couple of best practices for client-side code and server-side code.

 

For any .NET code

 

Client-side .NET code

 

Server-side .NET code

 

Multithreaded code in .NET

xlsgen supports multi-threading, but that does not mean it's allowed to do crazy things. To be accurate, xlsgen is free-threaded, as opposed to thread safe. It means that the underlying engines are designed to work in parallel, following the classic document per worker thread model.

A typical multithreaded application has a main thread, and a number of worker threads.

Among practices are :

Here is a full example of multi-threaded code written with C# :

using System;
using System.Threading;
using System.Runtime.InteropServices;
using xlsgen;


namespace XlsGenMultithreading
{
    /// <summary>
    /// Summary description for Class1.
    /// </summary>
    class Class1
    {
        IXlsEngine engine;

        [DllImport("kernel32.dll")]
        static extern uint GetCurrentThreadId();

        [DllImport(@"C:\program files\xlsgen\xlsgen.dll")]
        static extern IXlsEngine Start();

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [MTAThread]
        static void Main(string[] args)
        {
            Class1 c = new Class1();
            c.XlsgenMultithreading();
        }

        void XlsgenMultithreading()
        {
            engine = Start();

            for (int i = 0; i < 10; i++)
            {
                Thread t = new Thread(new ThreadStart(XlsgenWorkbook));
                t.Name = "thread" + i;
                t.Start();
            }
            Console.In.ReadLine();

            System.Runtime.InteropServices.Marshal.ReleaseComObject(engine);
        }

        void XlsgenWorkbook()
        {
            IXlsWorkbook wbk = engine.New( @"sample_" + Thread.CurrentThread.Name + "_" + GetCurrentThreadId() + ".xls" );
            wbk.StyleOption = xlsgen.enumStyleOption.styleoption_preserve;
            IXlsWorksheet wksht = wbk.AddWorksheet( "samplesheet" );
            wksht.set_Label(1,2, "Hello world!");
            wksht.set_ColWidth(2, 50);
            wbk.Close();
        }
    }
}

 

xlsgen documentation. © ARsT Design all rights reserved.