I'm sure all us C# guys, at some point or another, have looked 'over the fence' with envy at all those juicy Java libraries. In my case, recently I needed to export and import some Excel files, and found that there aren't really any good C# libraries for it. So i'd heard of IKVM and thought, why not give it a try? And it worked, really simply I might add! So here are the steps. Note that they're pretty generic across most libraries, but in my case this is all relating to the JXL library. Firstly download IKVM. I grabbed the ikvmbin-0.40.0.1.zip file. Then grab your JAR file from the library you want to use. Copy it into the ikvm\bin folder, and from the command prompt, do this: ikvmc.exe my-jar-file.jar -target:library This will (hopefully) produce a my-jar-file.dll file. This is your .Net compiled version of the library. To use this new library, you'll want to copy it into your project and add it as a reference to your .net application, and do the same with some of the dll's from the IKVM\bin folder. I needed the following dll's for JXL, but you may need more or less, depending on what subset of the java standard library your library uses: IKVM.OpenJDK.Core.dll IKVM.OpenJDK.Text.dll IKVM.OpenJDK.Util.dll IKVM.Runtime.dll Then, in my code, you can call the library from C#, just as if it was a .Net library. See the example for JXL below:
using System;
using jxl;
using jxl.write;

namespace JxlFromCSharp
{
  class Program
  {
    static void Main(string[] args)
    {
      WritableWorkbook book =
        Workbook.createWorkbook(new java.io.File("new.xls"));
      WritableSheet sheet = book.createSheet("First Sheet", 0);
      Label lbl = new Label(0, 2, "My Label");
      sheet.addCell(lbl);
      book.write();
      book.close();
    }
  }
}
As an aside, i've since found a really good native .Net excel library (also it's open source): http://npoi.codeplex.com/

Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.

Chris Hulbert

(Comp Sci, Hons - UTS)

iOS Developer in Sydney.

I have worked at places such as Google, News Corp, Fox Sports, NineMSN, FetchTV, Woolworths, and Westpac, among others. If you're looking for a good iOS developer, drop me a line!

Get in touch:
chris.hulbert@gmail.com
github.com/chrishulbert
linkedin
my resume



 Subscribe via RSS