Parsing spreadsheets with Clojure
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:
loadingWhew, 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:
loadingTo extract the data from the spreadsheet, we basically need to do three things:
- Load the file.
- Select the sheet we want to read.
- Specify the columns we are interested in.
Pretty straightforward:
loadingCalling read-rows will return a list of maps whose keys correspond to the values of the columns. Eg:
loadingThe last two are empty rows. We can leave those out pretty easily:
loadingThis will filter out all rows that do not have a value in column A.
Happy hacking!