Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use File Object instead filename #3

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 31 additions & 22 deletions src/bb_excel/core.clj
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
(ns bb-excel.core
(:require [clojure.data.xml :refer [parse-str]]
[clojure.java.io :refer [file]]
[clojure.java.io :as io]
[clojure.set :refer [rename-keys]])
(:import [java.time LocalDate Month]
[java.text SimpleDateFormat]
[java.time.format DateTimeFormatter]
[java.util TimeZone]
[java.util.zip ZipFile])
(:import [java.io File]
[java.time LocalDate Month]
[java.text SimpleDateFormat]
[java.time.format DateTimeFormatter]
[java.util TimeZone]
[java.util.zip ZipFile])
(:gen-class))

(set! *warn-on-reflection* true)
Expand Down Expand Up @@ -69,10 +70,10 @@
(defn get-sheet-names
"Retrieves a list of Sheet Names from a given Excel Spreadsheet
Returns nil if the file does not exist or a non-string is passed as the filename"
[filename]
(when (and (not-any? (fn [f] (f filename)) [nil? coll?])
[file]
(when true #_(and (not-any? (fn [f] (f filename)) [nil? coll?])
(.exists (file filename)))
(let [^ZipFile zf (ZipFile. ^String filename)
(let [^ZipFile zf (ZipFile. ^File file)
wb (.getEntry zf "xl/workbook.xml")
ins (.getInputStream zf wb)
x (parse-str (slurp ins))
Expand Down Expand Up @@ -159,8 +160,8 @@

(defn get-unique-strings
"Get dictionary of all unique strings in the Excel spreadsheet"
[filename]
(let [zf (ZipFile. ^String filename)
[file]
(let [zf (ZipFile. ^File file)
wb (.getEntry zf (str "xl/sharedStrings.xml"))
ins (.getInputStream zf wb)
x (parse-str (slurp ins))]
Expand All @@ -171,8 +172,8 @@

(defn get-styles
"Get styles"
[filename]
(let [zf (ZipFile. ^String filename)
[file]
(let [zf (ZipFile. ^File file)
wb (.getEntry zf (str "xl/styles.xml"))
ins (.getInputStream zf wb)
x (parse-str (slurp ins))]
Expand All @@ -184,24 +185,24 @@
(filter #((:xf tags) (:tag %)))
(mapv (comp :numFmtId :attrs)))))

(defn get-sheet
(defn get-sheet-from-file
"Get sheet from file"
([filename sheetname]
(get-sheet filename sheetname {}))
([filename sheetname options]
([^File file sheetname]
(get-sheet-from-file file sheetname {}))
([^File file sheetname options]
(let [opts (merge defaults options)
row (:row opts)
hdr (:hdr opts)
row (if (and hdr (zero? row)) 1 row)
rows (:rows opts)
fxn (:fxn opts)
cols (map fxn (:columns opts))
sheetid (:idx (first (filter #(= sheetname (:name %)) (get-sheet-names filename))))
zf (ZipFile. ^String filename)
sheetid (:idx (first (filter #(= sheetname (:name %)) (get-sheet-names file))))
zf (ZipFile. ^File file)
wb (.getEntry zf (str "xl/worksheets/sheet" sheetid ".xml"))
ins (.getInputStream zf wb)
dict (get-unique-strings filename)
styles (get-styles filename)
dict (get-unique-strings file)
styles (get-styles file)
xx (slurp ins)
x (parse-str xx)
d (->> x :content
Expand All @@ -215,7 +216,15 @@
dy (if (pos? rows)
(take rows (map #(rename-keys % h) dx))
(map #(rename-keys % h) dx))]
(if (empty? cols) dy (map #(select-keys % cols) dy)))))
(if (empty? cols) dy (map #(select-keys % cols) dy)))))

(defn get-sheet
"Get sheet from file"
([^String filename sheetname]
(get-sheet filename sheetname {}))
([^String filename sheetname options]
(let [file (io/file filename)]
(get-sheet-from-file file sheetname options))))

(defn get-sheets
"Get all or specified sheet from the excel spreadsheet"
Expand Down
Loading