Code Monkey home page Code Monkey logo

crystal_xlsx's Introduction

crystal_xlsx

A library to create xlsx (Excel 2006) file with styles, column width and freeze rows.

Installation

  1. Add the dependency to your shard.yml:

    dependencies:
      crystal_xlsx:
        github: confact/crystal_xlsx
  2. Run shards install

Usage

require "crystal_xlsx"

And then execute:

workbook = CrystalXlsx::Workbook.new

worksheet = workbook.add_worksheet("A worksheet")

worksheet.add_row(["Hello", "World"])
# or
# worksheet << ["Hello", "World"]

workbook.close("./hello_world.xlsx")

You can set the column width and freeze rows:

workbook = CrystalXlsx::Workbook.new

worksheet = workbook.add_worksheet("A worksheet")

worksheet.add_row(["Hello", "World"])

worksheet.freeze_row(1) # Freeze the first row

worksheet.columns_width = [10, 20] # Set the width of the first column to 10 and the second to 20

workbook.close("./hello_world.xlsx")

You can also set the style of the cells:

workbook = CrystalXlsx::Workbook.new

worksheet = workbook.add_worksheet("A worksheet")

style = workbook.add_format(
  font_name: "Arial",
  font_size: 12,
  bold: true,
  text_color: "FF0000",
  bg_color: "FFFF00",
  horizontal_align: "center",
  vertical_align: "center"
)

worksheet.add_row(["Hello", "World"], style)

workbook.close("./hello_world.xlsx")

You can get the result of the workbook as a string:

workbook = CrystalXlsx::Workbook.new

worksheet = workbook.add_worksheet("A worksheet")

worksheet.add_row(["Hello", "World"])

puts workbook.to_s

# or send it to a IO
workbook.to_io(STDOUT)

Known issues

  • The library does not support formulas
  • The library does not support images
  • The library does not support charts

Benchmark

As I made this library due to issues of ruby libraries being slow, I have made a benchmark to compare them. I compare it with the benchmarks at fast_excel that does a benchmark of 20k write rows and compare it with caxls and write_xlsx.

           FastExcel:        1.4 i/s
               Axlsx:        0.4 i/s - 3.46x  slower
          write_xlsx:        0.1 i/s - 17.04x  slower

And crystal_xlsx (Using Crystal's Benchmark module):

        write 20k rows   3.52  (284.13ms) (± 2.35%)  271MB/op

Development

Feel free to contribute to the project. You can run the tests with crystal spec.

Contributing

  1. Fork it (https://github.com/confact/crystal_xlsx/fork)
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request

Contributors

crystal_xlsx's People

Contributors

confact avatar

Watchers

 avatar

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.