This gem allows you to generate spreadsheets in the XML Spreadsheet format. It provides an api for constructing spreadsheet data and then uses that data to generate xml that can be interpreted by MS Excel.
** Note: this gem only generates XML according to a subset of the August 2001 XML Spreadsheet spec (msdn.microsoft.com/en-us/library/aa140066(office.10).aspx). It does not generate the more modern open office spreadsheet spec (xlsx).
gem install xmlss
require 'xmlss' workbook = Xmlss::Workbook.new workbook.worksheets # => [] sheet1 = Xmlss::Worksheet.new({ :name => 'Sheet 1' }) sheet1.table = Xmlss::Table.new sheet1.table.rows # => [] row1 = Xmlss::Row.new(1, 'some data', 54.54) row1.cells.count = 3 row1.cells.first.data # => 1 sheet1.table.rows << row1 workbook.worksheets << sheet1 workbook.to_xml # => "..." (XML Spreadsheet xml string)
see the examples folder for some use case examples.
Be aware this library only provides the basic, raw API for constructing spreadsheets using this spec and utilities to convert those objects to string xml data representing them. It does not provide any macro logic to aid in constructing the sheets. If you want a more convenient API for your use case, I suggest subclassing the objects and tailoring them to your needs.
The XML Spreadsheet spec and format are legacy and may have limited support depending on your version of MS Excel. For a more modern spreadsheet generation method, I suggest looking into Office Open XML Workbook format (en.wikipedia.org/wiki/Office_Open_XML).
These classes define how a spreadsheet is constructed.
-
styles: array, Xmlss:Style objects, default: []
-
worksheets: array, Xmlss::Worksheet objects, default: []
-
name: string
-
table: Xmlss::Table object
-
columns: array, Xmlss::Column objects, default: []
-
rows: array, Xmlss::Row objects, default: []
-
style_id: string
-
width: numeric
-
auto_fit_width: bool, default: false
-
hidden: bool, default: false
-
style_id: string
-
height: numeric
-
auto_fit_height: bool, default: false
-
hidden: bool, default: false
-
cells: array, Xmlss::Cell objects, default: []
-
style_id: string
-
data: Xmlss::Data object
-
href: string
-
merge_across: int, default: 0
-
merge_down: int, default: 0
-
type: :number, :date_time, :boolean, :string, :error, required
-
value: needs to respond to ‘to_s’
These classes define how a spreadsheet cells are styled.
-
id: string, unique identifier for the style
-
alignment: Xmlss::Style::Alignment object
-
borders: array, Xmlss::Style::Border objects, default: []
-
font: Xmlss::Style::Font object
-
interior: Xmlss::Style::Interior object
-
number_format: Xmlss::Style::NumberFormat object
-
protection: Xmlss::Protection object
-
horizontal: :automatic, :left, :center, :right, :default, default: :default
-
vertical: :automatic, :top, :center, :bottom, :default, default: :default
-
wrap_text: bool, default: false
-
rotate: int (-90 to 90)
-
position: :left, :top, :right, :bottom
-
color: hex string
-
style: :none, :continuous, :dash, :dot, :dash_dot, :dash_dot_dot, default: :continuous
-
weight: :hairline, :thin, :medium, :thick, default: :thin
-
bold: bool, default: false
-
color: hex string
-
name: string
-
italic: bool, default: false
-
size: int
-
strike_through: bool, default: false
-
shadow: bool, default: false
-
underline: :single, :double, :single_accounting, :double_accounting
-
alignment: :subscript, :superscript
-
color: hex string
-
pattern: symbol (see code for options), default: :default
-
pattern_color: hex string
-
format: :symbol (there are MANY different options for this, I suggest saving a spreadsheet in the XML format and seeing what Excel uses)
-
protected: bool, default: false
-
Full XML Spreadsheet spec: msdn.microsoft.com/en-us/library/aa140066(office.10).aspx
Copyright © 2011 Kelly D. Redding
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.