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.
Interop.xlsgen.dll
version. This file is produced by the .NET compiler, so rebuilding and redeploying is required.
Interop.xlsgen.dll
file with the following command-line : tlbimp xlsgen.dll /keyfile:mykey.snk /out:Interop.xlsgen.dll
, where mykey.snk
is your private key pair. This file should replace the unsigned Interop.xlsgen.dll
file automatically created by the Visual Studio environment when building your project.
System.Runtime.InteropServices.Marshal.ReleaseComObject(engine);
Session
object map. You can take a look at the ASP.NET 2.0 online spreadsheet sample for an example of that.
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.