NPOI: Generating Excel Files from C#

I’m constantly amazed at the time that Microsoft wastes on OLE automation, when it typically isn’t even the best way of integrating with Excel.  Easily the best way to generate (or read, for that matter) an Excel document is to use NPOI.  (There are competitors, such as ExcelLibrary, but they lack the depth of features of NPOI.)  The version on NuGet is pretty up to date.  The one catch is that the API is hermetic.  Here’s some tips:

  • You need to create a HSSFWorkbook.
  • You should create any styles and fonts before you start to process the cells.  (You don’t have to, but it’ll be easier to understand the code.)
  • The correct way to create a custom data format is workbook.CreateDataFormat().GetFormat(“0.00”), which will return a short.  You then assign the number to the dataformat of the cell style.

It’s also blindingly fast compared to most other techniques.  Finally, there’s quite extensive questions on stackoverflow about it.

Technorati Tags: ,

Gotcha: Huge Window Sizes in Caliburn Micro

If you bind a grid to a large data set, you will inevitably create a control larger than is comfortable to show on the screen.  By default, if you have this the root view model in Caliburn Micro, you will end up with a window that is so large that it is hard to manipulate on the screen.  This is because, by default, the windows that CM creates are set to size to the control.  If you want to change this behaviour, you will need to change the SizeToContent property of the window to Manual.  To do this, you need to do the following:

First, you need a custom window manager.  If you’re using an IoC container, you can simply register an implementation of IWindowManager.  I recommend subclassing the default WindowManager class.  If not, you can simply override GetInstance in the Bootstrapper; this also has to advantage of suppressing annoying on-boot exceptions.

Secondly, override EnsureWindow (as of 1.1, it’s slightly different in 1.0) in the window manager.  You’ll note the window manager is aware of the view model it contains, which allows for more customized approaches depending on your requirements.  A final gotcha: whereas most of the code is found in the Silverlight implementation, WindowManager is completely different between WP7, WPF and Silverlight and is contained in different projects.

Technorati Tags:

How To Configure Leiningen to Use An HTTP Proxy

I spent a couple of hours banging my head against a wall over this.  Leiningen doesn’t respect the HTTP_PROXY environment variable as most Unix programs do.  I finally asked on the Clojure IRC channel and got the answer from manutter: leiningen, of course, uses maven for its dependency management.  Maven, in turn, doesn’t respect the environment variable.  Instead you need to configure a specific file in your ~/.m2 directory.

Hope this helps someone.

Technorati Tags: ,