Code Monkey home page Code Monkey logo

xlsx_reader's Introduction

XlsxReader logo

XlsxReader

Build status

An XLSX reader in Elixir.

Features:

  • Accepts XLSX data located on the file system or in memory
  • Automatic type conversions (numbers, date & times, booleans)
  • Optional support for arbitrary precision decimal numbers
  • Straightforward architecture: no ETS tables, no race-conditions, no manual resource management

The docs can be found at https://hexdocs.pm/xlsx_reader.

Installation

Add xlsx_reader as a dependency in your mix.exs:

def deps do
  [
    {:xlsx_reader, "~> 0.8.0"}
  ]
end

Run mix deps.get in your shell to fetch and compile XlsxReader.

Examples

Loading from the file system

{:ok, package} = XlsxReader.open("test.xlsx")

XlsxReader.sheet_names(package)
# ["Sheet 1", "Sheet 2", "Sheet 3"]

{:ok, rows} = XlsxReader.sheet(package, "Sheet 1")
# [
#   ["Date", "Temperature"], 
#   [~D[2019-11-01], 8.4], 
#   [~D[2019-11-02], 7.5], 
#   ...
# ]

Loading from memory

blob = File.read!("test.xlsx")

{:ok, package} = XlsxReader.open(blob, source: :binary)

Loading all sheets at once

{:ok, sheets} = XlsxReader.sheets(package)
# [
#   {"Sheet 1", [["Date", "Temperature"], ...]}, 
#   {"Sheet 2", [...]}, 
#   ...
# ]

Loading sheets selectively

{:ok, sheets} = XlsxReader.sheets(package, only: ["Parameters", ~r/Sheet \d+/], except: ["Sheet 2"])
# [
#   {"Parameters", [...]}, 
#   {"Sheet 1", [...]}, 
#   {"Sheet 3", [...]}, 
#   {"Sheet 4", [...]}, 
#   ...
# ]

Loading all sheets at once concurrently

{:ok, sheets} = XlsxReader.async_sheets(package)
# [
#   {"Sheet 1", [["Date", "Temperature"], ...]}, 
#   {"Sheet 2", [...]}, 
#   ...
# ]

Using arbitrary precision numbers

{:ok, rows} = XlsxReader.sheet(package, "Sheet 1", number_type: Decimal)
# [
#   ["Date", "Temperature"], 
#   [~D[2019-11-01], %Decimal{coef: 84, exp: -1, sign: 1}], 
#   [~D[2019-11-02], %Decimal{coef: 75, exp: -1, sign: 1}], 
#   ...
# ]

Access cell formulas

{:ok, rows} = XlsxReader.sheet(package, "Sheet 1", cell_data_format: :cell)
# [
#   [%Cell{value: 1234.0, formula: "SUM(B1, B10)", ref: "A1"}, ...], 
#   ...
# ]

Development

Benchmarking

  1. mix run benchmark/init.exs to create the benchmarking dataset
  2. mix run benchmark/run.exs to run the Benchee suite

Contributors

In order of appearance:

License

Copyright 2020 Xavier Defrang

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

xlsx_reader's People

Contributors

03juan avatar darraghenright avatar dharness avatar evaldobratti avatar kianmeng avatar paranojik avatar patrykwozinski avatar xavier avatar zachliss avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

xlsx_reader's Issues

ISO8601 date conversion

Hi there!

Great library! I'm just trying it out here, nice clear API and documentation.

I'm not sure if this is an issue or not—apologies if not, but I figured I'd ask to clarify:

In an example .xlsx file I was testing I noticed an ISO8601 formatted date (e.g: 2020-05-17 was returned as 43968.0.

This confused me at first but I quickly discovered that this is a serial date, thanks to the Dates And Times in Excel article you linked in conversion.ex.

The same date formatted as 17/5/2020 in another cell was returned as a date sigil, so I guess I expected that the ISO8601 formatted date would be returned as a date sigil too. Or is this expected behaviour?

Thanks!

Convert integer date value to an actual `Date`

In the Excel sheet I have a date column for which I only get integer values, e.g. 41760. If I understand Excel correctly I can just do Date.new!(1900, 1, 1) |> Date.add(41760) and get ~D[2014-05-03] as the actual date. Is that correct? Maybe there is a nicer way to convert those integers into dates?

Empty rows are skipped even with empty_rows: true

Setup: empty sheet with a value in B2 cell

image
file: xlsx_reader_empty_row_test.xlsx

iex(14)> {:ok, package} = XlsxReader.open("xlsx_reader_empty_row_test.xlsx")
{:ok, ...

Current behaviour

iex(15)> XlsxReader.sheet(package, "Sheet1", empty_rows: true)
{:ok, [["", "b2"]]}

Expected behaviour

iex(15)> XlsxReader.sheet(package, "Sheet1", empty_rows: true)
{:ok, [[...], ["", "b2"]]}

First row is ignored with empty_rows: true.

I need to keep empty rows because l need to transform [["", "b2"]] into %{2 => %{"B" => "b2"}} or similar so I could address cells by row/column.

Any advice?

"no match of right hand side value: :error"

While reading an XLSX (which I cannot unfortunately share, but will be able to re-test on it if we figure this out), I got the following error:

** (MatchError) no match of right hand side value: :error
    (xlsx_reader 0.7.0) lib/xlsx_reader/parsers/worksheet_parser.ex:308: XlsxReader.Parsers.WorksheetParser.convert_current_cell_value/1
    (xlsx_reader 0.7.0) lib/xlsx_reader/parsers/worksheet_parser.ex:162: XlsxReader.Parsers.WorksheetParser.add_cell_to_row/1
    (xlsx_reader 0.7.0) lib/xlsx_reader/parsers/worksheet_parser.ex:109: XlsxReader.Parsers.WorksheetParser.handle_event/3
    (saxy 1.5.0) lib/saxy/emitter.ex:16: Saxy.Emitter.emit/3
    (saxy 1.5.0) lib/saxy/emitter.ex:7: Saxy.Emitter.emit/4
    (saxy 1.5.0) lib/saxy/parser/builder.ex:1191: Saxy.Parser.Binary.close_tag_name/6
    (saxy 1.5.0) lib/saxy.ex:194: Saxy.parse_string/4

I am using v0.7.0.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.