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

Setting a hyperlink text color in axlsx

I'm trying to set the foreground color of text in a hyperlink cell but it doesn't seem to work. Using something like: sheet["A1"].color = "0000FF" works fine for a normal cell, but not for a hyperlinked cell This code simply creates a link to cell…
user1717468
  • 61
  • 1
  • 2
5
votes
0 answers

Excel stacked bar chart not plotted correctly

I am using Axlsx gem for generating the excel charts on ruby on rails. My requirement is to generate a stacked bar chart but it is generating in a staircase format refer the image here. To generate the chart I have used the following…
Aditya Tiwari
  • 204
  • 4
  • 17
5
votes
0 answers

Xlsx file gives error on MS Excel once it is zipped using rubyzip and unzipped on windows

Actually my requirement is to zip some files in some folders. I have already many files saved on my server and associated with some objects. So I have to zip all the files related to one object somewhat like this: Main Folder Sub Folder Another…
Deepesh
  • 6,138
  • 1
  • 24
  • 41
5
votes
2 answers

Axlsx gem: is it possible to apply a background color to individual cells?

It is possible to apply a font color to individual cells, without creating a style: Axlsx::Package.new do |p| p.workbook.add_worksheet(:name => "test") do |ws| ws.add_row ["a", "b", "c"] ws.add_row ["d", "e", "f"] ws.add_row…
aaandre
  • 2,502
  • 5
  • 33
  • 46
5
votes
0 answers

How can I speed up add_row calls in axlsx

I am creating about 10,000 rows with about 50 columns of data. My code looks like this: Axlsx::Package.new do |spreadsheet| fields.keys.each do |question| sortedFields = fields[question].keys.sort …
Mike McKay
  • 2,388
  • 1
  • 29
  • 32
5
votes
0 answers

How do I auto-fit a row height after merging cells?

I am generating a spreadsheet using: Rails 3.2.12 with the axlsx gem (version 2.0.0) There are cells which I want to have the text wrap and the height of the row adjust to match. This works when I use the following: p = Axlsx::Package.new wb =…
Syntch
  • 51
  • 1
  • 3
5
votes
1 answer

Exporting to Excel - What to do with timestamps?

I've got Time objects that I'm writing to an Excel file. I'm using the axlsx library. The class that converts dates to the cell data is DateTimeConverter, which turns it into a float timestamp. The times are displayed as mm/DD/YYYY HH:MM:SS as…
user684934
4
votes
3 answers

How to format Excel cell as currency using axlsx gem - Ruby on Rails

I learnt that you could format the types of cells in an Excel like this: :types => [nil, :integer, :string] However I looked at the list of all types possible and I find only integer and float but I don't see a currency option. How can I format a…
m.beginner
  • 389
  • 2
  • 18
4
votes
0 answers

Underline does not work in axlsx in ruby on rails

I am using axlsx gem to generate spreadsheets. I am using various styles like bold font, borders, background colors etc which all work fine. But when I use underline style, it does not work at all (I don't get any error though). bold_underline =…
Biju
  • 820
  • 1
  • 11
  • 34
4
votes
1 answer

rails to_xlsx Couldn't find all Vulnerabilities with 'id': (all, {}) (found 0 results, but was looking for 2)

I have an Rails app (ruby 2.0.0, Rails 4.2.1). I would like to export data to excel using acts_as_xlsx gem. Here is my controller: class VulnerabilitiesController < ApplicationController before_action :set_vulnerability, only: [:show, :edit,…
basq
  • 41
  • 1
4
votes
1 answer

Devise authentication error with ActionController::Responder and axlsx_rails gem

Added an export-to-xlsx option with the axlsx and axlsx_rails gems to an existing Rails 4.2.0 app that uses Devise for authentication. Export to xlsx now works properly: app/controllers/cases_controller.rb: class CasesController <…
Collin Meyers
  • 345
  • 3
  • 9
4
votes
2 answers

Rails axlsx_rails gem. Insert new row between existing rows

I am generating .xlsx files using axlsx_rails gem based on axlsx. I am receiving rows as array and drawing them like this: # Workbook, sheet and styles creations left... data["config"].each do |item| sheet.add_row item.each_with_index.map{|row,…
vvahans
  • 1,849
  • 21
  • 29
4
votes
0 answers

Specify Intervals in x-axis labels using axlsx gem

I am working on the xlsx report generation, and for that I am using axlsx gem. I am facing 2 issues, one of them is as follow and other is asked in separate question Secondary Axis in axlsx charts. The issue I am facing to specify intervals between…
jbmyid
  • 1,985
  • 19
  • 22
4
votes
1 answer

With Axlsx, is it possible to define the data type for an entire column?

I have a requirement to export an Excel template, with just a header row containing the column names, and each of the columns set to the appropriate data type for the entire spreadsheet. Is this possible to do using the axlsx gem? I tried this as…
4
votes
1 answer

Rails axlsx gem load file

I need to load an existing xlsx file and edit it. All the example I found so far include creating a file from zero and editing it. I already have the file and I just want to edit it. Anyone that done this before?
Trt Trt
  • 5,330
  • 13
  • 53
  • 86
1
2
3
18 19