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
3
votes
0 answers

axlsx ruby gem: is there a way to add a text box?

I would like to add a title in large type at the top of the generated spreadsheet. The equivalent of Excel's native Insert > Text Box, positioned top center. Is there a way to achieve this via axlsx? How? If this is impossible, what would be an…
aaandre
  • 2,502
  • 5
  • 33
  • 46
3
votes
2 answers

Applying several styles to a cell in Excel spreadsheet with AXLSX gem

I am using the AXLSX Ruby gem to generate Excel reports and I'm having a problem applying several styles to cells in a modular way. Here's an example that has just two styles ("on background" and "bold on background"), require 'axlsx' axlsx =…
sakovias
  • 1,356
  • 1
  • 17
  • 26
3
votes
3 answers

Rails Axlsx New Line in Cell

Is there a way that I can add a new line to a cell using the Axlsx gem in Rails? So basically replicating in Excel once you enter a value you can do a Alt + Enter to add additional text to the new line in the cell. I tried sheet.add_row ["Testing…
Tom Hammond
  • 5,842
  • 12
  • 52
  • 95
3
votes
1 answer

Ruby Divide Always Returning Zero in Rails Axlsx Export

I cannot for the life of me figure out why dividing isn't working in this Aslsx export. I can add, subtract, multiple and do modulus division fine, but when I try to do answer_number / question_number it is always coming out as 0. Any ideas what I'm…
Tom Hammond
  • 5,842
  • 12
  • 52
  • 95
3
votes
1 answer

Axlsx_rails. Set height of multiple rows at once

I'm generating *.xlsx files using axlsx_rails based on xlsx gem. I'm setting height for single row like this: sheet.add_row [1,2,3,4,5,6], :style => predefined_style, :height => 14.3 How could I set height for a batch of rows, and if it…
vvahans
  • 1,849
  • 21
  • 29
3
votes
2 answers

generating xls file using axlsx

is there a way to generate XLS file using 'axlsx' gem? I'm already using this gem to generate xlsx files, and don't want to move it all to a different gem, especially since axlsx is much easier to work with.
avital
  • 559
  • 1
  • 7
  • 19
3
votes
1 answer

AXLSX merge cells inside a style

I'm using ruby gem axlsx and want to know if there's a way to set merge columns inside a style? Today i doing like this: sheet.merge_cells "A1:E1" sheet.add_row [I18n.t('foo.some_label').upcase], style: [title] sheet.merge_cells "B2:E2" ... I want…
Rafael
  • 45
  • 1
  • 6
3
votes
2 answers

axlsx serialize spreadsheet to string

For testing purposes, I'd like to serialize an axlsx spreadsheet to a string. The axlsx documentation indicates it is possible to "Output to file or StringIO". But I haven't found documentation or a code sample that explains how to output to a…
Russell Silva
  • 2,772
  • 3
  • 26
  • 36
3
votes
3 answers

Ruby AXLSX gem merging more then one set of cells

I'm using Ruby version 1.9.3 and Rails version 3.0.20 with the axlsx ruby gem to generate a file which has a rather complicated structure, involving cellmerging. The gem is great but I hit the wall when trying to merge more the 1 set of cells in one…
tpopov
  • 131
  • 2
  • 5
3
votes
1 answer

Inverted Axis in AXLSX Bar Chart

I'm using this very straightforward code (practically the same as the bar chart examples here, however I am noticing that the Axes are inverted. The data that should be on the Val axis is shown on the Cat axis. This is also true for the example…
MalSu
  • 541
  • 3
  • 11
3
votes
1 answer

how to reference a cell programmatically in a loop with axlsx

I want to programmatically add rows to my worksheet that reference other cells within the same row. I've done this: require 'axlsx' Axlsx::Package.new do |p| p.workbook.add_worksheet(name: 'Foo') do |sheet| sheet.add_row(['i', 'i*2']) …
Paul Brannan
  • 1,625
  • 20
  • 20
3
votes
1 answer

Adding comments to a cell in an excel sheet using axlsx gem

I am using axlsx gem to create excel sheets. I am trying to generate a report in which there is a heading. I would like to add comments to each cell that contains heading text. I can do it normally in Libre office calc by right clicking on the cell…
Sri Murthy Upadhyayula
  • 22,620
  • 1
  • 18
  • 20
2
votes
1 answer

Rails axlsx: How to insert value into new cell in existing row

I have 2 table with one-to-many relationship, activities table and direct_activities table In the axslx file, I have code like this wb = xlsx_package.workbook activities = Activity.all.includes(:direct_activities) wb.add_worksheet(name: "Sheet 1")…
Loadd64
  • 57
  • 8
2
votes
0 answers

Permission denied @ rb_sysopen from Ruby gem writing to filesystem

Ruby 2.4.0 Rails 5.2.3 I have a Rails ActiveJob generating an xlsx report using the Axlsx gem. When it tries to write the file to the file system with: axlsx.serialize("#{Rails.root}/tmp/#{@report_name}.xlsx") It gives the following error:…
ThriceGood
  • 1,633
  • 3
  • 25
  • 43
2
votes
0 answers

Adding global font size styling to Axlsx sheet

I would like to set the sheet-wide default font size set to 8pt, instead of the default 11pt. Is there a way to set a global style using the Axlsx gem (implemented with Axlsx-Rails)? I've looked at the axlsx_styler gem, which looks like it solves a…
simonlehmann
  • 852
  • 1
  • 10
  • 27