Skip to content

Instantly share code, notes, and snippets.

@mjul
Last active July 5, 2016 09:48
Show Gist options
  • Save mjul/c77004eb8fe811d009b02c8ba0506760 to your computer and use it in GitHub Desktop.
Save mjul/c77004eb8fe811d009b02c8ba0506760 to your computer and use it in GitHub Desktop.
Convert XLS file to CSV (here just the A, B and C columns) (Clojure)
(comment
(defproject importer "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.8.0"]
[org.clojure/data.csv "0.1.3"]
[dk.ative/docjure "1.10.0"]]
:main importer.core)
)
(ns importer.core
(:gen-class))
(use 'dk.ative.docjure.spreadsheet)
(require '[clojure.data.csv :as csv]
'[clojure.java.io :as io])
(defn load-xls [fname sheet]
(->> (load-workbook fname)
(select-sheet sheet)
(select-columns {:A :a, :B :b :C :c})))
(defn save-csv! [fname rows]
(with-open [out-file (io/writer fname)]
(csv/write-csv out-file rows :separator \;)))
(defn xls-to-csv!
[input-file output-file]
(->> (load-xls input-file #"^Data\s*")
(map (juxt :a :b :c))
(save-csv! output-file)))
(xls-to-csv! "data.xls" "data.csv")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment