Questions tagged [axlsx]

Ruby, JRuby and Rubinius Office Open XML (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.

Axlsx: Office Open XML Spreadsheet Generation

IRC: irc.freenode.net / #axlsx
Git: http://github.com/randym/axlsx
Twitter: https://twitter.com/#!/morgan_randy
Google Group: https://groups.google.com/forum/?fromgroups#!forum/axlsx

Author: Randy Morgan

Copyright: 2011 - 2017
License: MIT License

Latest Version: 3.0.0
Ruby Version: 2.2.7, 2.3.4, 2.4.1
JRuby Version: 1.9 modes
Rubinius Version: rubinius 3 * lower versions may run, this gem always tests against head.
Release Date: September 12th 2013

If you are working in rails, or with active record see: acts_as_xlsx

acts_as_xlsx is a simple ActiveRecord mixin that lets you generate a workbook with:

Posts.where(created_at > Time.now-30.days).to_xlsx

** and **

http://github.com/straydogstudio/axlsx_rails Axlsx_Rails provides an Axlsx renderer so you can move all your spreadsheet code from your controller into view files. Partials are supported so you can organize any code into reusable chunks (e.g. cover sheets, common styling, etc.) You can use it with acts_as_xlsx, placing the to_xlsx call in a view and add ':package => xlsx_package' to the parameter list. Now you can keep your controllers thin!

There are guides for using axlsx and acts_as_xlsx here: http://axlsx.blog.randym.net

If you are working with ActiveAdmin see: activeadmin-axlsx

It provides a plugin and dsl for generating downloadable reports.

The examples directory contains a number of more specific examples as well.

Synopsis

Axlsx is an Office Open XML Spreadsheet generator for the Ruby programming language. With Axlsx you can create excel worksheets with charts, images (with links), automated and fixed column widths, customized styles, functions, tables, conditional formatting, print options, comments, merged cells, auto filters, file and stream serialization as well as full schema validation. Axlsx excels at helping you generate beautiful Office Open XML Spreadsheet documents without having to understand the entire ECMA specification.

sample

Feature List

  1. Author xlsx documents: Axlsx is made to let you easily and quickly generate professional xlsx based reports that can be validated before serialization.

  2. Generate 3D Pie, Line, Scatter and Bar Charts: With Axlsx chart generation and management is as easy as a few lines of code. You can build charts based off data in your worksheet or generate charts without any data in your sheet at all. Customize gridlines, label rotation and series colors as well.

  3. Custom Styles: With guaranteed document validity, you can style borders, alignment, fills, fonts, and number formats in a single line of code. Those styles can be applied to an entire row, or a single cell anywhere in your workbook.

  4. Automatic type support: Axlsx will automatically determine the type of data you are generating. In this release Float, Integer, String, Date, Time and Boolean types are automatically identified and serialized to your spreadsheet.

  5. Automatic and fixed column widths: Axlsx will automatically determine the appropriate width for your columns based on the content in the worksheet, or use any value you specify for the really funky stuff.

  6. Support for automatically formatted 1904 and 1900 epochs configurable in the workbook.

  7. Add jpg, gif and png images to worksheets with hyperlinks

  8. Reference cells in your worksheet with "A1" and "A1:D4" style references or from the workbook using "Sheet1!A3:B4" style references

  9. Cell level style overrides for default and customized style objects

  10. Support for formulas, merging, row and column outlining as well as cell level input data validation.

  11. Auto filtering tables with worksheet.auto_filter as well as support for Tables

  12. Export using shared strings or inline strings so we can inter-op with iWork Numbers (sans charts for now).

  13. Output to file or StringIO

  14. Support for page margins and print options

  15. Support for password and non password based sheet protection.

  16. First stage interoperability support for GoogleDocs, LibreOffice, and Numbers

  17. Support for defined names, which gives you repeated header rows for printing.

  18. Data labels for charts as well as series color customization.

  19. Support for sheet headers and footers

  20. Pivot Tables

  21. Page Breaks

Installing

To install Axlsx, use the following command:

$ gem install axlsx

Examples

The example listing is getting overly large to maintain here. If you are using Yard, you will be able to see the examples in line below.

Here's a teaser that kicks about 2% of what the gem can do.

Axlsx::Package.new do |p|
  p.workbook.add_worksheet(:name => "Pie Chart") do |sheet|
    sheet.add_row ["Simple Pie Chart"]
    %w(first second third).each { |label| sheet.add_row [label, rand(24)+1] }
    sheet.add_chart(Axlsx::Pie3DChart, :start_at => [0,5], :end_at => [10, 20], :title => "example 3: Pie Chart") do |chart|
      chart.add_series :data => sheet["B2:B4"], :labels => sheet["A2:A4"],  :colors => ['FF0000', '00FF00', '0000FF']
    end
  end
  p.serialize('simple.xlsx')
end

Please see the examples examples for more.

There is much, much more you can do with this gem. If you get stuck, grab me on IRC or submit an issue to GitHub. Chances are that it has already been implemented. If it hasn't - let's take a look at adding it in.

Documentation

This gem is 100% documented with YARD, an exceptional documentation library. To see documentation for this, and all the gems installed on your system use:

gem install yard kramdown

yard server -g

Specs

This gem has 100% test coverage using test/unit. To execute tests for this gem, simply run rake in the gem directory.

272 questions
4
votes
1 answer

UTF-8 error when generating Excel file using axlsx gem

I am trying to use the axlsx gem to generate an Excel file. This is a Ruby Rails application where the user views a report in the browser and can click a link to export the data to Excel. However, I am getting the following error: …
4
votes
2 answers

axslx: undefined method for yard

I'm running "yard server -g" but it's only generating the Table of Contents for axlsx. When I click on a class I get: undefined method `new' for nil:NilClass. Any idea why? (Fwiw, other gems are working fine in Yard server)
Snowcrash
  • 80,579
  • 89
  • 266
  • 376
4
votes
2 answers

Rails AXLSX gem trying to merge cells of last row created

I have an axlsx file with some logic in there that creates rows. I have a check in there, that when a certain condition is met, i want to merge the last cells created. How can i do this? The only way that i've seen how to merge cells is by…
Catfish
  • 18,876
  • 54
  • 209
  • 353
4
votes
1 answer

Read and edit an excel template using the Axlsx gem?

I have an excel template I'd like to load and then edit in ruby. I've looked into the Axlsx gem but I don't see a way to read in a file and edit? Am I missing something?
rmontgomery429
  • 14,660
  • 17
  • 61
  • 66
4
votes
1 answer

Column & row collapsing in xlsx file

I've been asked to create an Excel (.xlsx) report with collapsed rows and columns. The application is in ruby/rails using axlsx gem for excel generation. For the moment I cannot have the collaped symbol "+" facing the hidden row or column that…
Michael A.
  • 2,288
  • 1
  • 18
  • 21
4
votes
1 answer

How to add data labels using axlsx

I'm trying to generate a pie chart using axlsx for ruby. Everything is working fine but I can't seem to get data labels to appear for each "pie slice". What I'm shooting for is more or less like this image. (ie it should have the data labels and…
jmichels
  • 43
  • 3
4
votes
2 answers

xlsx issue: create excel sheet with large number of rows

I am using Axlsx to create an excel file. For a small dataset, it works fine. But once the dataset gets big, it just hangs. I ran strace on the process, it was doing a lot brk. a = Axlsx::Package.new book = a.workbook book.add_worksheet(:name =>…
user612308
  • 1,813
  • 5
  • 23
  • 22
3
votes
1 answer

Change text color in an Excel cell based on a specific condition - Ruby on Rails

I am using axlsx gem to generate Excel sheets in Ruby on Rails. I have this line which adds a row to an Excel sheet current_row = sheet.add_row [v1, v2, v3], :style => style1 Now, v1, v2 & v3 in the above code are currency values. And any of these…
Biju
  • 820
  • 1
  • 11
  • 34
3
votes
0 answers

Axlsx Multiselect Dropdown List

How can multi-select dropdown be generated using gem axlsx so that mutiple entries can be selected using axlsx data_validation and formula.
Athul Santhosh
  • 246
  • 3
  • 11
3
votes
1 answer

axlsx_rails gem: Columns width issue

I'm using axlsx_rails gem to write in .xlsx file. I have multiple columns in spreadsheet based on form fields. It can vary. I would like to set the column width for all the columns based on data available. I have used: col_widths= [10,20,30,40,50]…
Hasmukh Rathod
  • 1,108
  • 1
  • 14
  • 20
3
votes
2 answers

axlsx / axlsx_rails dependancy / conflict issues

I am trying to add simple Excel file downloads to my Rails app with https://github.com/randym/axlsx and https://github.com/straydogstudio/axlsx_rails but I can't seem to get the dependancies sorted out. Here is where I started out with my gem…
Dan Tappin
  • 2,692
  • 3
  • 37
  • 77
3
votes
1 answer

How to write array to Excel with differing styles per column using Axlsx

I would like to be able to use the Axlsx gem to write out an Excel spreadsheet with different styles applied to different columns. I am writing an Array of Hashes an entire row at a time and, as a result, I cannot seem to format the columns…
Kurt W
  • 321
  • 2
  • 15
3
votes
1 answer

Ruby Axlsx how to add row with merged cells

I want to generate the below excel: I tried bellow code row = [1, 2, [31, 32]] p = Axlsx::Package.new wb = p.workbook wb.add_worksheet(:name => "Sheet1") do |sheet| sheet.add_row row end But I get the bellow result |column1|column2| column3 | |…
fudy
  • 1,540
  • 5
  • 25
  • 41
3
votes
0 answers

how we can add the background color in full sheet using axlsx gem?

Here is my code and I want to add background color in full sheet not in a specific row or column. xlsx_package = Axlsx::Package.new wb = xlsx_package.workbook wb.add_worksheet(:name => "Basic Worksheet") do |sheet| sheet.add_row ["First…
Chitra
  • 1,294
  • 2
  • 13
  • 28
3
votes
1 answer

Passing multiple arguments to variable argument ruby method using array

I'm working with a ruby gem right now that takes in a variable number of arguments (to be specific, it's the axlsx gem). I'm using the column_widths function, defined as: def column_widths(*widths) widths.each_with_index do |value, index| …
mosquito242
  • 353
  • 5
  • 17
1 2
3
18 19