我有一个 Excel 文件。我导入了sheetjs(https://sheetjs.com/)。我想读取工作表数据。我如何在 Clojurescript 中使用 Sheetjs 读取 excel?
编写的项目这应该会让您走向正确的方向 - 它会在与index.html(应用程序的根文件夹)相同的文件夹中打开一个名为datadump.xlsx的文件。易于适应通过表单上传文件。在主命名空间中,您只需调用
(data/read-local-file)
。
(ns data
(:require [reagent.core :as r]
[reagent.dom :as rd]
[re-frame.core :as rf]
["xlsx" :as XLSX]))
(defn read-sheet [wb sheet-name]
(let [sheet (aget (. wb -Sheets) sheet-name)
data (XLSX/utils.sheet_to_json sheet #js {:header 1})
dataclj (js->clj data)
kk (map keyword (first dataclj))
outdata (into [] (for [line (rest dataclj)] (zipmap kk line)))]
(rf/dispatch [:source-data outdata])))
(defn read-local-file []
(let [xhr (js/XMLHttpRequest.)]
(.open xhr "GET" "datadump.xlsx" true)
(set! (.-responseType xhr) "blob")
(set! (.-onload xhr)
(fn []
(let [reader (js/FileReader.)]
(.readAsArrayBuffer reader (.-response xhr))
(set! (.-onload reader)
(fn [evt]
(let [ab (-> evt .-target .-result)
wb (XLSX/read ab #js {:type "array" :cellDates true})
sn (. wb -SheetNames)]
(read-sheet wb (first sn))))))))
(.send xhr)))