<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Honolulu Hacker &#187; ruby spreadsheet</title>
	<atom:link href="http://honoluluhacker.com/tag/ruby-spreadsheet/feed/" rel="self" type="application/rss+xml" />
	<link>http://honoluluhacker.com</link>
	<description>Tech, Linux, Rails by Honululu-based Kevin English</description>
	<lastBuildDate>Sat, 24 Sep 2011 22:53:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.2</generator>
		<item>
		<title>Example SQL Report with the Ruby Spreadsheet Gem</title>
		<link>http://honoluluhacker.com/2009/06/17/example-sql-report-with-the-ruby-spreadsheet-gem/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=example-sql-report-with-the-ruby-spreadsheet-gem</link>
		<comments>http://honoluluhacker.com/2009/06/17/example-sql-report-with-the-ruby-spreadsheet-gem/#comments</comments>
		<pubDate>Wed, 17 Jun 2009 20:44:49 +0000</pubDate>
		<dc:creator>kenglish</dc:creator>
				<category><![CDATA[Mysql]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[ruby spreadsheet]]></category>

		<guid isPermaLink="false">http://honoluluhacker.com/?p=694</guid>
		<description><![CDATA[If you want to create a Excel reports for your users, this can be done rather easily in Ruby using the Spreadsheet Gem. def spreadsheet_report&#40;excel_filename, worksheet_name, column_order, result&#41; book = Spreadsheet::Workbook.new sheet1 = book.create_worksheet :name =&#62; worksheet_name &#160; rownum = 0 for column in column_order sheet1.row&#40;rownum&#41;.push column end for row in result rownum += 1 [...]]]></description>
			<content:encoded><![CDATA[<p>If you want to create a Excel reports for your users, this can be done rather easily in Ruby using the <a href="http://rubyforge.org/projects/spreadsheet">Spreadsheet Gem</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">def</span> spreadsheet_report<span style="color:#006600; font-weight:bold;">&#40;</span>excel_filename, worksheet_name, 
                       column_order, result<span style="color:#006600; font-weight:bold;">&#41;</span>
    book = <span style="color:#6666ff; font-weight:bold;">Spreadsheet::Workbook</span>.<span style="color:#9900CC;">new</span>
    sheet1 = book.<span style="color:#9900CC;">create_worksheet</span> <span style="color:#ff3333; font-weight:bold;">:name</span> <span style="color:#006600; font-weight:bold;">=&gt;</span> worksheet_name
&nbsp;
    rownum = <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> column <span style="color:#9966CC; font-weight:bold;">in</span> column_order
      sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span>rownum<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">push</span> column
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> row <span style="color:#9966CC; font-weight:bold;">in</span> result
      rownum <span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">1</span>
      <span style="color:#9966CC; font-weight:bold;">for</span> column <span style="color:#9966CC; font-weight:bold;">in</span> column_order
        sheet1.<span style="color:#9900CC;">row</span><span style="color:#006600; font-weight:bold;">&#40;</span>rownum<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">push</span> row<span style="color:#006600; font-weight:bold;">&#91;</span>column<span style="color:#006600; font-weight:bold;">&#93;</span>.<span style="color:#0000FF; font-weight:bold;">nil</span>? ? <span style="color:#996600;">'N/A'</span> : row<span style="color:#006600; font-weight:bold;">&#91;</span>column<span style="color:#006600; font-weight:bold;">&#93;</span>
      <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    book.<span style="color:#9900CC;">write</span> <span style="color:#996600;">&quot;#{excel_filename}.xls&quot;</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>Here&#8217;s what the code would like in your rake task</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;">column_order = <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">&quot;Name&quot;</span>, <span style="color:#996600;">&quot;DOB&quot;</span>, <span style="color:#996600;">&quot;Rank&quot;</span>,<span style="color:#996600;">&quot;Hire Date&quot;</span>, 
                <span style="color:#996600;">&quot;Height&quot;</span>, <span style="color:#996600;">&quot;Weight&quot;</span> <span style="color:#006600; font-weight:bold;">&#93;</span>
&nbsp;
sql =<span style="color:#006600; font-weight:bold;">&lt;&lt;-</span><span style="color:#9966CC; font-weight:bold;">END</span>
  <span style="color:#CC0066; font-weight:bold;">SELECT</span> name AS Name,
   date_of_birth AS DOB, 
   rank AS Rank,
   hire_date Hire <span style="color:#CC00FF; font-weight:bold;">Date</span>,
   height AS Height,
   weight AS  Weight
  FROM fire_fighters
  ORDER BY name
<span style="color:#9966CC; font-weight:bold;">END</span>
&nbsp;
conn  = <span style="color:#6666ff; font-weight:bold;">ActiveRecord::Base</span>.<span style="color:#9900CC;">connection</span>
result    = conn.<span style="color:#9900CC;">select_all</span><span style="color:#006600; font-weight:bold;">&#40;</span>sql<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
excel_filename = <span style="color:#996600;">&quot;FireFighterReport#{Time.year}&quot;</span> 
worksheet_name = <span style="color:#996600;">&quot;FireFighter Report #{Time.year}&quot;</span>
spreadsheet_report<span style="color:#006600; font-weight:bold;">&#40;</span>excel_filename, worksheet_name, 
                   column_order, result<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Now, that&#8217;s easy!</p>
<p><a href="http://spreadsheet.rubyforge.org/">Ruby Spreadsheet Gem  Documentation</a> </p>
]]></content:encoded>
			<wfw:commentRss>http://honoluluhacker.com/2009/06/17/example-sql-report-with-the-ruby-spreadsheet-gem/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

