19

I need to read and write to Excelx file with ruby code, Tried with spreadsheet it does not support xlsx format, Roo is also to read the file and not write to xlsx.

Is there any gem/plugin which will write to Excelx?

1

9 Answers 9

20

For the task of writing/exporting xlsx files, Axlsx is the most feature complete library I've found. It does not support reading/importing xlsx files, though.

https://github.com/randym/axlsx

Here's the author's description:

xlsx generation with charts, images, automated column width, customizable styles and full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification. Check out the README for some examples of how easy it is. Best of all, you can validate your xlsx file before serialization so you know for sure that anything generated is going to load on your client's machine.

Sign up to request clarification or add additional context in comments.

6 Comments

I'll also add that the gem is very well documented and actively maintained.
The library is write only and doesn't support reading existing xlsx files at all. :-(
@morganchristiansson You're right. I hadn't noticed as I only need writing. Sorry for the confusion :(
Axlsx gem is very good and seems fully feature
This gem doesn't seem to be supported any more, last version was in 2015. Pay attention that it exposes your project to a vulnerability in rubyzip gem. See github.com/randym/axlsx/issues/536
|
9

You can try this gem

https://github.com/harvesthq/simple_xlsx_writer

And

https://github.com/cxn03651/writeexcel/

1 Comment

As a note, simple_xlsx_writer has "no formatting or styles included". writeexcel write old-style xls files, not xlsx format.
7

seems RubyXL does both, read AND write https://github.com/gilt/rubyXL haven't used it yet though..

Comments

3

An XLSX file is essentially a zipped collection of XML files. If you don't find suitable gem, you may try generating them manually using Ruby, possibly using an empty template saved from Excel. We used to do that, worked pretty well, unfortunately that code is not open-sourced.

But eventually, we needed more control over how the result looks so we made .NET based solution with Excel automation.

Comments

2

I have found a lot of success utilizing WIN32OLE with excel. I always find myself back at rubyonwindows.blogspot.com for examples.

Comments

2

Adding to the list of above gems, Roo(https://github.com/roo-rb/roo) is a popular ruby gem that is used to manage and work with excel and xlsx files.

1 Comment

Judging from their readme, it's read-only access.
1

I found the interfaces of all the existing ruby excel libraries to be very un-ruby-like, and especially inaccurate when determining cell types when reading files.

So, if you're looking for something that's (IMO) simpler, I've successfully used the combination of simple_xlsx_reader and simple_xlsx_writer.

I'm the author of simple_xlsx_reader, and as of the time of this writing I don't have tests in simple_xlsx_reader that assert all use cases for reading files generated by simple_xlsx_writer, so I can't say it's fully supported, but I can say I've deployed a production application that used both libraries back-to-back for various integration tests, without issue.

Comments

0

Check with this gem, i is very useful to generate any difficult format/layout gem 'axlsx' , github: 'randym/axlsx', branch: 'master' and refer these link's for Documentationaxlsx-documentation

Comments

0

To export (csv & xslx) without any gem we can use-

https://gorails.com/episodes/export-to-csv

This link shows how to export in csv, below is how we can get xlsx.

  1. open controller
  2. find method which calling to_csv method,

    send_data @models.to_csv, filename: "Contract-#{Date.today}.csv"

  3. write as-

    send_data @models.to_csv, filename: "Contract-#{Date.today}.xlsx"

That worked for me!!!

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.