Skip to content

Add Sheet manipulation example to README. #18

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
40 changes: 40 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Lein:
- [What are the options for styling?](#what-are-the-options-for-styling?)
- [Grids](#grids)
- [Templates](#templates)
- [Accessing The `Sheet`](#accessing-the-sheet)
- [Roadmap](#roadmap)
- [Development](#development)
- [Unit Tests](#unit-tests)
Expand Down Expand Up @@ -271,6 +272,45 @@ For example, you can try:
(excel/append! new-data template "filled-in-template.xlsx"))
```

### Accessing The `Sheet`

There is not a way to directly access the `Sheet` object using the API. Instead,
following the function `excel-clj.file/write*` as an example, open an Excel file
and create the `Sheet`.

```clojure
(require '[clojure.java.io :as io])
(require '[excel-clj.core :as excel])
(require '[excel-clj.file :as file])
(require '[excel-clj.poi :as poi])
(import '[org.apache.poi.ss.usermodel Sheet])

(let [worksheet-data (excel/table-grid [{:name "Trip 1"
:miles 34.5
:max-speed 15.2}
{:name "Trip 2"
:miles 14.1
:max-speed 35.2}
{:name "Trip 3"
:miles 18.9
:max-speed 21.1}])
;; create the .xlsx file
xlsx-filename (file/temp ".xlsx")
excel-file (io/file xlsx-filename)]
;; get the `Sheet` object
(with-open [poi-writer (poi/writer excel-file)]
(let [sheet-writer (poi/sheet-writer poi-writer "Sheet1")
worksheet ^Sheet (:sheet sheet-writer)]
;; manipulate the `Sheet`
(doto worksheet
(.setColumnWidth 1 500)
(.createFreezePane 0 1))
;; write the data to the `Sheet`
(file/write-rows! sheet-writer worksheet-data)))
;; optionally open the created file
(file/open excel-file))
```

## Roadmap

- A way to read in a saved workbook to the `{sheet-name [[cell]]}` format. I'm
Expand Down