A library to create xlsx (Excel 2006) file with styles, column width and freeze rows.
-
Add the dependency to your
shard.yml
:dependencies: crystal_xlsx: github: confact/crystal_xlsx
-
Run
shards install
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)
- The library does not support formulas
- The library does not support images
- The library does not support charts
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
Feel free to contribute to the project. You can run the tests with crystal spec
.
- Fork it (https://github.com/confact/crystal_xlsx/fork)
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create a new Pull Request
- Håkan Nylén - creator and maintainer