Tuesday, 15 January 2008

Using ExcelWriter on 64bit Kit

I've recently been migrating some web apps to 64bit Windows 2003 Server. So far all testing has gone pretty well, with most pure ASP.Net apps running on the kit with little or no code changes.

One issue you may have though, is if your app uses any COM interop with old 32bit components. Earlier ExcelWriter versions used exactly this approach, providing dot net wrapper classes for what were essentially 32bit COM objects on the box. You will find these will no longer work on the new kit (unless you use WOW64 emulation) so the key is to ensure you now use the "pure dot net" versions of ExcelWriter.

Conversion is quite straight forward. Here's some old "dot net wrapped" 32bit only code:

SAExcelApplicationDotNet Xlw = new SAExcelApplicationDotNet();  // old 32bit wrapper ExcelApplication
SoftArtisans.ExcelWriter.SAWorksheet Sheet = Xlw.Worksheets[1];
Sheet.Cells[1, 1].Value = 20;
Xlw.Save(@"test.xls", SoftArtisans.ExcelWriter.SASaveMethod.saOpenInPlace, SoftArtisans.ExcelWriter.SAFileFormat.saFileFormatExcel2000);

The above code also requires ASPCompat=True in your aspx page to ensure single-threaded apartment (STA) mode is used for that page's thread. Not ideal for web apps, see COM Compatibility Page for full run down.

Anyhow, here's the pure dot net version:

ExcelApplication Xlw = new ExcelApplication();   // new pure dot net ExcelApplication
Workbook WB = Xlw.Create();
Worksheet WS = WB.Worksheets[0];
WS.Cells[1, 1].Value = 20;
Xlw.Save(WB, Page.Response, "test.xls", false);

Easy... even I can follow the above!