Open Source Fast Ruby Spreadsheet Generator Library
Ruby API that programmatically supports creating and manipulating spreadsheet documents. It allows to protect spreadsheets, apply custom styles & formatting, inserting images, and hyperlinks.
What is caxlsx?
caxlsx is a very useful and feature rich open source Ruby library that helps developers to generate powerful apps for creating and manipulating spreadsheet with ease. The spreadsheet generator library caxlsx helps the developer to make stunning looking Open XML Spreadsheet documents without having the complete knowledge of ECMA specification.
caxlsx library is very powerful but yet very simple to configure. It has included several important features for Excel spreadsheet generation and processing such as adding new worksheet, renaming a worksheet, add charts to worksheet, inserting images and links, manage column & rows widths, customized styles, tables handling, printing support, merging cells, apply filters, add comments and much more.
The library also included some advanced features while handling spreadsheet files, such as adding headers and footers, handling PivotTables, protecting your sheets using passwords, Auto filtering tables, and many more.
Getting Started with caxlsx
The recommend way to install caxlsx library is by using RubyGems. Please use the following command for smooth installation.
Install caxlsx via RubyGems
gem 'caxlsx'
You can download the compiled shared library from the GitHub repository and install it.
Create Spreadsheet with Charts via Ruby
The caxlsx library makes it easy for software programmers to create Excel spreadsheets inside their own Ruby applications. The library supports adding charts to their spreadsheet with just a couple of lines of code. It enables developers to generate 3D pie, line, scatter, and bar charts easily. The library allows users to build charts based on the custom data as well as generate charts without any data in their worksheets. You can easily customize the gridlines, label rotation, and much more. Here is a very simple example that shows how to create a worksheet and two rows of data inside Ruby Apps.
How to Create a Worksheet with Rows of Data via Ruby Library?
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
wb.add_worksheet(name: 'Basic Worksheet') do |sheet|
sheet.add_row ['First', 'Second', 'Third']
sheet.add_row [1, 2, 3]
end
p.serialize 'basic_example.xlsx'
Protect Spreadsheet via Ruby API
The open source caxlsx library gives Ruby programmers the ability to protect the spreadsheet by applying passwords inside their own applications. It is a very useful technique that prevents other people from making any changes or viewing, moving, or deleting important data from a worksheet. The library has included support for creating password protected as well as non-password worksheets with just a couple of lines of code.
Apply Custom Styles to Worksheets
Custom styles are a very convenient way to save users time when formatting their worksheets. The caxlsx library has provided complete functionality for applying custom style and formatting to Excel Spreadsheet using Ruby code. The library has provided consistent formatting across a range of cells. Developers can easily style borders, use alignment, apply fills, select fonts, and number formats in a single line of Ruby code.
How to Add Styles to a Whole Column via Ruby API?
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
s = wb.styles
percent = s.add_style num_fmt: 9, bg_color: '0000FF'
highlight = s.add_style bg_color: 'FF0000'
default_green = s.add_style bg_color: '00FF00'
wb.add_worksheet(name: 'Custom column types') do |sheet|
sheet.add_row ['A', 'B', 'Percent', 'Hidden', 'E']
# Apply default style to columns A and E - it will be used for cells defined after this line and all undefined cells in the column
sheet.column_styles default_green, nil, nil, nil, default_green
# Override the default column style with the usual style optional param
sheet.add_row [ 1, 2, 0.3, 4, 5.0], style: [highlight]
sheet.add_row [0.1, 2, 0.2, 4, 5.0]
sheet.add_row [0.1, 2, 0.1, 4, 5.0]
# Apply the percent style to the cells already defined in a column at index 2 skipping the first row.
sheet.col_style 2, percent, row_offset: 1
sheet.add_row [0.1, 2, 0.0, 4, 5.0]
# Apply the hidden attribute to the column at index 3
sheet.column_info[3].hidden = true
end
p.serialize 'column_styles_example.xlsx'
Inserting Images and Hyperlinks to Spreadsheets
The free caxlsx library has provided complete support for adding and modifying images inside their worksheets with just a couple of Ruby commands. You can use popular image formats such as JPG, GIF, and PNG images inside a worksheet. You can also insert images with hyperlinks. The library also allows replacing an existing image or modifying its size and place.
How to Add External Image to a Spreadsheet via Ruby Library?
require 'axlsx'
p = Axlsx::Package.new
wb = p.workbook
image = File.expand_path('assets/image1.jpeg')
wb.add_worksheet(name: 'Image') do |sheet|
sheet.add_image(image_src: image, start_at: 'B2', width: 100, height: 100)
sheet.add_image(image_src: image, start_at: 'E1', end_at: 'G3')
sheet.add_image(image_src: 'https://via.placeholder.com/150.png', remote: true, start_at: 'E4', end_at: 'G6')
end
p.serialize 'image_example.xlsx'