Skip to content

Instantly share code, notes, and snippets.

@henrik
Last active August 29, 2015 14:24
Show Gist options
  • Save henrik/99d7abff11d2daf128e7 to your computer and use it in GitHub Desktop.
Save henrik/99d7abff11d2daf128e7 to your computer and use it in GitHub Desktop.
ESV - Excel generation with the ease of CSV generation.
# ESV - Excel generation with the ease of CSV generation.
#
# By Henrik Nyh 2015-07-01 under the MIT license.
# Using a lot of code from LivingSocial's Excelinator, also under the MIT license: https://github.com/livingsocial/excelinator/blob/master/lib/excelinator/xls.rb
require "spreadsheet"
class ESV
def self.generate
instance = new
yield(instance)
instance.render
end
def initialize
@data_rows = []
end
def <<(row)
@data_rows << row
end
def render
book = Spreadsheet::Workbook.new
sheet = book.create_worksheet
@data_rows.each_with_index do |data_row, index|
row = sheet.row(index)
row.push(*data_row)
end
content = ""
fake_file = StringIO.new(content)
book.write(fake_file)
content
end
end
# :D Will write this if I make it into a gem…
require "esv"
data = ESV.generate do |esv|
esv << ["a", "b"]
esv << [1, 2]
end
File.write("/tmp/test.xls", data)
# OS X
system("open", "/tmp/test.xls")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment