Last active
August 29, 2015 14:24
-
-
Save henrik/99d7abff11d2daf128e7 to your computer and use it in GitHub Desktop.
ESV - Excel generation with the ease of CSV generation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# :D Will write this if I make it into a gem… |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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