Skip to content

XLS generation from html tables through XERB templates

Notifications You must be signed in to change notification settings

brunosiqueira/excellent

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

EXCELlent is a custom template handler (XERB) for specifying XLS files just as regular XHTML tables.
This is kind of limited at the moment, but damn - this is 0.0.1 alpha release :)

Currently EXCELlent (or XERB) supports

* table, tr, td, th tags
* table name property for naming sheet
* font-size, font-weight and color css attributes of cells
* colspan for td and th tags


First things first - installation

    ruby script/plugin install git:https://github.com/bsiqueira/excellent.git

EXCELlent relies on spreadsheet and xml-simple gems, so if you haven't already...


    gem install spreadsheet
    gem install xml-simple


Then in your controller, add new respond_to format


    def index
        @fruits = [{:name => "apple", :color => "green"},{:name => "peach", :color => "orange"}]

        respond_to do |format|
            format.html
            format.xls # Here it is !
        end
    end



Then we need our view. Just create file named index.xls.xerb and create a simple table


    <table name="Fruits">

        <tr>
            <td></td>
        </tr>
	<tr>
	    <td colspan="2" style="background-color:blue;"> Fruits</td>
	</tr>
        <tr>
            <td style="font-size:20px;">Name</td>
            <td>Color</td>
        </tr>

        <% @fruits.each do |fruit| %>
            <tr>
                <td style="color:red;font-weight:bold;"><%= fruit[:name] %></td>
                <td><%= fruit[:color] %></td>
            </tr>
        <% end %>

    </table>

To create a xls file with many worksheets, just wrap different tables inside li tags.
The file will have as many worksheets as tables.

    <ul>
        <li>
            <table name="Fruits">
                <tr>
                    <td></td>
                </tr>
                <tr>
                    <td colspan="2" style="background-color:blue;"> Fruits</td>
                </tr>
                <tr>
                    <td style="font-size:20px;">Name</td>
                    <td>Color</td>
                </tr>

                <% @fruits.each do |fruit| %>
                    <tr>
                        <td style="color:red;font-weight:bold;"><%= fruit[:name] %></td>
                        <td><%= fruit[:color] %></td>
                    </tr>
                <% end %>
            </table>
        </li>
        <li>
            <table name="Total">
                <tr>
                    <td>
                        <%= @fruits.count%> fruits
                    </td>
                </tr>
            </table>
        </li>
    </ul>


From now on, simple link

    <%= link_to 'xls', fruits_path(:format => :xls) %>

will enable you to download Excel version of your table :)

About

XLS generation from html tables through XERB templates

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Ruby 100.0%