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

Formatting a cell as Text using the axlsx spreadsheet ruby gem?

I'm using the axlsx ruby gem to create Excel-compatible .xlsx files. I can't figure out how to override the cell type that is generated by it's automatic type detection. For Active Record model attributes of type string the gem is setting the Excel…
platforms
  • 2,666
  • 1
  • 18
  • 23
11
votes
2 answers

export to public folder using axlsx

I am using the axlsx gem to create excel files. I get them serialized in my project home directory as an xlsx file. But I want the file to be created in the public folder of my rails app, or directly downloadable by the user without saving it in the…
mukesh
  • 127
  • 1
  • 9
9
votes
3 answers

Rails ActionController unknown format

I am trying to render a xlsx file. But I keep getting a 406/UnknowFormat. I have done the right setup, maybe im missing something? Rails 4.2 app gem 'axlsx' gem "axlsx_rails" gem 'zip-zip' config/initializers/mime Mime::Type.register…
Seal
  • 1,060
  • 1
  • 12
  • 31
8
votes
1 answer

Setting Excel column widths correctly when adding to spreadsheet via Axlsx

I'm trying to format my report and making sure the columns have a correct width and I can't seem to make it happen with auto_width. With this code, this is the kind of report I get . Notice the space that is way to long for an auto_width, since if…
Nicolas Belley
  • 803
  • 2
  • 12
  • 29
8
votes
4 answers

axlsx : acts_as_xlsx : how do i add style to only one cell?

I see methods for col_style and row_style and add_conditional_formatting, but can't figure out how to set just one cell. In my example, col 1 is a date and column 2 is a percent. when i highlight the background of a row, i lose the date/percent…
elubin
  • 101
  • 1
  • 3
7
votes
2 answers

Axlsx - Formatting text within a cell

I can't seem to find any information on whether it's possible to fill a single cell with multiple formatting options. For example, I want cell A1 to be filled with the text: "Hello world, this is excel" Is this possible and if so what syntax do I…
Steve
  • 2,764
  • 4
  • 27
  • 32
7
votes
1 answer

How to I add a hyperlink to a cell in axlsx?

With the spreadsheet gem, you can run Spreadsheet::Link.new('http://hyperlinkhere.com', 'Some words') to make a spreadsheet with a cell containing the string "Some words" with a hyperlink leading to "http://hyperlinkhere.com." What's the axlsx…
Username
  • 3,463
  • 11
  • 68
  • 111
7
votes
1 answer

Modify specific cell value using Axlsx gem given the column number and row numer

I am trying to use asxlx gem to write an excel sheet but I have failed miserably trying to access individual cell values.. For example for do I get the value of the cell 2 in column 2 How do I change the value of cell 10 in column 19 ??
Renato Co
  • 137
  • 1
  • 4
7
votes
3 answers

Gem axlsx how to set font font_name?

I use axlsx gem to work with xlsx file. Please help me to set font in cells of sheet. item_style = s.add_style :b => false, :sz => 9, :font_name => 'courier', :alignment => { :horizontal => :left, :vertical => :center, :wrap_text => true} row…
greenif
  • 1,055
  • 1
  • 12
  • 25
6
votes
0 answers

axlsx_rails excel report format breaks in windows

Am using axlsx_rails gem with activeadmin to export formatted excel report This format is working fine in Ubuntu LibreOffice but the styles breaks in windows OS. This is the proper aligned format in Ubuntu. But below this image is from Windows…
Mani David
  • 1,382
  • 1
  • 14
  • 30
6
votes
1 answer

How to format currency columns in Axlsx?

I am using ruby Axlsx gem for writing my clients accounts data in xlsx. Now I am appending " Dr" string to all debit amounts which is making amount as a string e.g. "1000 Dr". I want to append " Dr" but the amount should still be an integer and…
Umesh Malhotra
  • 967
  • 1
  • 10
  • 25
6
votes
1 answer

How do I get the row index after I do an .add_row using the axlsx gem?

I'm using the axlsx gem to create a XLSX file and have the following line in my code: ws.add_row( "xyz" ) Is there a way to get the row index of the row I had just added? I might have to add a row later.
Evilmuffin
  • 214
  • 4
  • 14
6
votes
1 answer

Axlsx merge cells based on row-column number like in Spreadsheet gem

I have been using the Spreadsheet gem to write xls files. With the Spreadsheet gem, merging the cells can be done dynamically based on the row and column number like this merge_cells(start_row, start_col, end_row, end_col) My Code snippet …
Yugendran
  • 172
  • 2
  • 9
6
votes
1 answer

AXLSX: Parsing xlsx file for rspec tests

Any idea how to write view specs (presence of headers, rows, etc.) for a generated xlsx file from the xlsx gem? Not sure if I'm doing it correctly in the first place but here's what I have so far RSpec.describe "spreadsheet.xlsx.axlsx", :type => …
sams
  • 188
  • 1
  • 10
6
votes
2 answers

Rails Axlsx Column Width And Wrap Text

Using Axlsx some of my cells are set to wrap text which works great. The problem I'm running into is how the auto-width of the cells seems to be calculated. It seems like what's happening is initially the data comes in like this: cell line1 cell…
Tom Hammond
  • 5,842
  • 12
  • 52
  • 95
1
2 3
18 19