Skip to content

Instantly share code, notes, and snippets.

@chaserx
Created June 30, 2009 22:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chaserx/138469 to your computer and use it in GitHub Desktop.
Save chaserx/138469 to your computer and use it in GitHub Desktop.
#!/usr/local/bin/ruby
require "rubygems"
require "fastercsv"
require "html/table"
include HTML
# new file
outfile = File.open("outfile.html", "w")
# Create a new table
# Explicit syntax
table = HTML::Table.new{ |t|
t.border = 1
t.cellpadding = 5
t.cellspacing = 0
}
# setup this variable for the loop
header_row = false
# open and loop thru each line of the CSV file
FasterCSV.foreach("memberlist.csv") do |row|
# use row here...
# push each row into the table
if header_row
table.push Table::Row.new{ |r|
r.content = row
}
else
new_header = Table::Row.new
row.each { |e|
temp = Table::Row::Header.new(e)
new_header.push temp
}
table.push new_header
# set header_row so that the loop executes the if statement not the else
header_row = true
end
end
# send formatted table into the outfile
outfile << table.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment