Parsing spreadsheets with Clojure

Roberto Guerra

 2 min read

Reading from an Excel file is surprisingly easy in clojure. We'll see an example in groovy and then compare it with one in clojure.

Groovy In groovy we can read an Excel sheet like this:

loading

Whew, that is a lot of code. Event though we wrote it in a more terse language than java, it is still quite long and error prone. Granted, we are using poi directly, compared to using a third party library like the one we'll use with clojure.

Clojure

In clojure we can use a third party library that is a wrapper for poi: Docjure. We can include this a dependency in our project.clj file:

loading

To extract the data from the spreadsheet, we basically need to do three things:

  1. Load the file.
  2. Select the sheet we want to read.
  3. Specify the columns we are interested in.

Pretty straightforward:

loading

Calling read-rows will return a list of maps whose keys correspond to the values of the columns. Eg:

loading

The last two are empty rows. We can leave those out pretty easily:

loading

This will filter out all rows that do not have a value in column A.

Happy hacking!