Tweetegy On the edge of chaos with Blockchain, Dapps, DeFi, JavaScript

| About | Search | Archive | Github | RSS |

How to create an excel spreadsheet using Java (JExcel actually)

If you want to manipulate an Excel spreadsheet using .NET it is very easy to do. If your not convinced by that statement, I will show you how in a separate post. However, the purpose of this post is to create an Excel spreadsheet and populate it with values using something other than .NET. There may be many reasons why you would want to do this and mine was simply because I wanted to stick to using Linux (Ubuntu 9.10 Server).

I gave up with trying it out in Mono – some basic ADO.NET code I had written just would not run! So I turned to Java! Someone on my team recommended I try JExcel and so I did. It turned out to be a simple excercise to spike. Here is the class:


import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;

public class JExcelSample
 public static void main(String args[]) {
   File f = new File("JExcelSample.xls");
   WritableWorkbook w = Workbook.createWorkbook(f);
   WritableSheet s = w.createSheet("Demo", 0);
   s.addCell(new Label(0, 0, "Hello World"));
  } catch (Exception e)

To compile and run the above Java program you will need to download and save jxl.jar to your /usr/lib directory and run the following statement in your terminal:

javac -cp "/usr/lib/jxl.jar"
java -cp "/usr/lib/jxl.jar" JExcelSample

That’s it! After you have run the program you will see the JExcelSample.xls file and if you open it you will see that it contains one worksheet called Demo with one entry “Hello World”. Note that you are not required to have Excel installed on your machine to do any of the above. The code runs just fine without and the spreadsheet will open in Spreadsheet by default.