<?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>Tyler Clemons &#187; Excel</title>
	<atom:link href="http://www.tylerclemons.com/tag/excel/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.tylerclemons.com</link>
	<description>tylerclemons.com</description>
	<lastBuildDate>Thu, 27 Oct 2011 17:06:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>OS Independent Excel Reports</title>
		<link>http://www.tylerclemons.com/os-independent-excel-reports/</link>
		<comments>http://www.tylerclemons.com/os-independent-excel-reports/#comments</comments>
		<pubDate>Sat, 28 Jun 2008 20:18:49 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[win32]]></category>
		<category><![CDATA[Add new tag]]></category>
		<category><![CDATA[Excel]]></category>
		<category><![CDATA[spreadsheet]]></category>

		<guid isPermaLink="false">http://tylershome.nfshost.com/?p=32</guid>
		<description><![CDATA[I ran into this problem awhile ago. I was tasked with making Excel reports using Ruby on Rails. Since I was running on Windows, I just used Win32ole. It worked quite well until I remembered that Win32 is not platform independent.  Basically, if the system is hosted on a non-Windows machine the Excel Report will [...]]]></description>
			<content:encoded><![CDATA[<p>I ran into this problem awhile ago. I was tasked with making Excel reports using Ruby on Rails. Since I was running on Windows, I just used Win32ole. It worked quite well until I remembered that Win32 is not platform independent.  Basically, if the system is hosted on a non-Windows machine the Excel Report will fail.  Great.  But, Ruby isn&#8217;t out for the count yet.  I found a pretty cool spreadsheet generator at RubyForge.  After installing spreadsheet, it is pretty simple to use.  You can either install it by downloading it from the web, click <a href="http://rubyforge.org/projects/spreadsheet/" target="_blank">HERE</a>, or use <a href="http://rubyforge.org/frs/?group_id=126" target="_blank">ruby gems</a> by typing this on the command line: &#8220;gem install spreadsheet-excel&#8221;</p>
<p><span id="more-13"></span></p>
<div id="codebox" style="overflow: auto; "><em>require &#8220;spreadsheet/excel&#8221;</em></p>
<p><em><span style="color: #ff0000;">#create the workbook and a worksheet</span><br />
workbook = <span style="color: #00ff00;">Spreadsheet</span>::<span style="color: #00ff00;">Excel</span>.new(&#8221;reports/file.xls&#8221;)<br />
</em>worksheet = workbook.add_worksheet(&#8221;worksheetname&#8221;)</p>
<p><em><span style="color: #ff0000;">#make some formats</span><br />
headers = <span style="color: #00ff00;">Format</span>.new(<span style="color: #339966;">:color</span> =&gt; &#8220;black&#8221;,<span style="color: #339966;"> :bold</span> =&gt;<span style="color: #3366ff;"> true</span>)</em></p>
<p><em>data = <span style="color: #00ff00;">Format</span>.new(<span style="color: #339966;">:color</span> =&gt; &#8220;black&#8221;, <span style="color: #339966;">:bold</span> =&gt; <span style="color: #3366ff;">false</span>, <span style="color: #339966;">:align</span> =&gt; &#8220;right&#8221;)</em></p>
<p><span style="color: #ff0000;"><em>#spreadsheet uses numbers for rows and columns i.e. A1 = 00</em></span></p>
<p><em>column = 0<br />
row = 0</em></p>
<p><span style="color: #ff0000;"><em>#write some headers from some header object, assume this is an array</em></span></p>
<p><em>header.each |field|</em></p>
<p style="padding-left: 30px;"><em>worksheet.write(column,row,field.chomp,top)<br />
column+=1</em></p>
<p><em><span style="color: #ff00ff;">end</span></em></p>
<p><em><span style="color: #ff0000;">#now insert data so move down a row</span><br />
column = 0 </em></p>
<p><em>row = 1</em></p>
<p><span style="color: #ff0000;"><em>#the object list is an array<br />
#each entry is a result of the query against the database </em></span></p>
<p><em>object_list.each <span style="color: #ff00ff;">do</span> |object|</em></p>
<p style="padding-left: 30px;"><em>headers.each <span style="color: #ff00ff;">do</span> |value|</em></p>
<p style="padding-left: 60px;"><em>worksheet.write(row,column,object[value],data) </em></p>
<p style="padding-left: 60px;"><em>column +=1</em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">end</span><br />
row +=1</em></p>
<p><em><span style="color: #ff00ff;">end</span></em></p>
<p><em>#finished so close<br />
workbook.close</em></div>
<p>Pretty simple. I like the formatting features. It is very easy to switch a format style after defining a particular format object. One of the design decisions that can be argued is how to actually write the data. An alternate approach is to write the headers one at a time, and as each header is written, iterate through the objects that we wish to write. The described method is inefficient because it ignores how Ruby stores its arrays, row-major order.</p>
<p>An alternate approach to calling the write method is to pass an array.  Unfortunately, when objects are returned by using the find method, the fields do not return in a specific order so there is no guarantee the headers we wrote at the top of the spreadsheet will be in the same order as the objects that were returned. I deployed a version of this with Ruby Rails.</p>
<p>In my own experience, my new function had surpassed my old Win32 based function in both speed and portability thanks to spreadsheet. I recommend using this method even if your project is slated for a windows box with office installed.  Unless you need formulas, then you are out of luck <img src='http://tylershome.nfshost.com/home/public/wp-includes/images/smilies/icon_sad.gif' alt=':(' class='wp-smiley' /> </p>
<p>The example is almost finished. I encapsulated the above function in a method. You can decide where to implement it, either in one of the helper controllers or in the current controller. In my implementation, a user navigates to the reports page, navigates some Ajax controls and submits, using a button that calls the function below, a desired report. The view for display_printout is left blank.</p>
<div id="codebox" style="overflow: auto; ">
<p><em><span style="color: #ff00ff;">def</span> <span style="color: #0000ff;">display_printout</span></em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff0000;">#I named the excel report create_printout<br />
#I included some error handling and a success returns true</span><br />
<span style="color: #ff00ff;">if</span> create_printout</em></p>
<p style="padding-left: 60px;"><em>send_file &#8220;reports/report.xls&#8221;</em></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">else</span></em></p>
<p style="padding-left: 60px;"><span style="color: #ff0000;"><em>#display some sort of error message</em></span></p>
<p style="padding-left: 30px;"><em><span style="color: #ff00ff;">end</span></em></p>
<p><em><span style="color: #ff00ff;">end</span></em></div>
]]></content:encoded>
			<wfw:commentRss>http://www.tylerclemons.com/os-independent-excel-reports/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

