RailsXLS Revived October 2nd, 2008

RailsXLS is a plugin to generate Excel files from a .rxls view. It uses a Java Bridge to talk to the Jakarta POI library. I’ve used this plugin in a few projects and it works great for generating .xls files.

On a recent project I once again needed an “Export to Excel” feature. The project is on Rails Edge, and apparently there were some changes to how TemplateHandlers are handled since the plugin was written. I dove into the code and recreated the plugin for Rails Edge / 2.2.

You can find the new plugin on github

It’s usage is quite easy:

# in your environment.rb
Mime::Type.register "application/excel", :xls

# in controller
def index
  @clients = Client.all
  respond_to do |speaks|
    speaks.html
    speaks.xls { render :layout => false }
  end
end

# in index.xls.rxls
sheet = workbook.createSheet("Client List")

@clients.each_with_index do |client, index|
  row = sheet.createRow(index)
  row.createCell(0).setCellValue(client.id)
  row.createCell(1).setCellValue(client.name)
end

As you can see there are still some rough patches in the view. I’m working on a wrapper around this, so all obvious tasks can be easily performed.

Any comments/suggestions for the plugin are very welcome! A big thanks to Venkata Subramaniyan for writing the initial plugin, which can be found here

tags: , , , l

12 Responses to “RailsXLS Revived”

  • 16 days ago Jim said

    I never understand why it has to be a genuine XLS for a use case such as this. If you make a CSV and the user opens it in MS Excel, it will display the exact same. Why the bulk of POI and Java etc?

  • 16 days ago Jan De Poorter said

    Jim, this is a dumbed down example. A CSV is great for this use case. The project needed different tabs and colour marked cells, which is quite difficult with CSV ;-)

  • 16 days ago Arya said

    Unrelated question/comment, it seems the timestamps (or its display) is some how messed up, because these order of events doesn’t make sense:

    34 minutes ago Jim said I never…

    about 2 hours ago Jan De Poorter said Jim, this is…

  • 16 days ago Roland said

    You may find IE screws around with respond to in this instance, always returning you the Excel by default (i.e. when no explicit format is provided).

    I’ve blogged about this here, with the solution

  • 6 days ago Xavier Noria said

    There are two gotchas regarding CSV and Excel.

    First of all the PITA of encodings. You cannot generate a CSV with a robust character encoding for the client (unless you control the client of course). OpenOffice documentation claims that some sort of Unicode works, but in my Mac Excel expects MacRoman, and in my Windows Excel expects Latin1. I bet Excel expects other encodings in other countries.

    Second gotcha is that Excel does not create an association with .csv files. So people cannot double-click the file, nor do they see an Excel icon. You can certainly open the CSV and with proper row and column separators it works out-of-the box. But some end-users will not expect this way to operate if you claim you export to Excel. By the way those separators are different in Mac/Windows as well.

    If you send a CSV file with .xls extension Excel presents an import wizard because it only assumes there’s some text there to interpret, which pretty much discards this as a viable approach. It works, but it is strange.

    I am now dumping tables in Excel’s XML format using raw builder templates and some peeking at hand-made sheets. That way I can reliably output UTF8. Drawback there is that OpenOffice does not understand those.

  • 6 days ago Chris O'Sullivan said

    Hey, so any pointers on how to get this to work on v2.1?

  • 6 days ago Chris O'Sullivan said

    So, I’ve created a really rough version of this that seems to work with Rails 2.1.

    Find it here: http://github.com/thechrisoshow/railsxls/tree/2.1

    To install do the following: In your plugins directory do this: git clone git://github.com/thechrisoshow/railsxls.git railsxls git fetch git checkout –track -b 2.1 origin/2.1

    And, it should work…maybe

  • 6 days ago Chris O'Sullivan said

    Blast, I screwed up the previous comment:

    To install the 2.1 version do the following:

    In your plugins directory do this:

    git clone git://github.com/thechrisoshow/railsxls.git

    cd railsxls

    git fetch

    git checkout –track -b 2.1 origin/2.1

  • 5 days ago Igor said

    Thanks for the RailsXLS. But it doesn’t work with rails 2.1

    I need to implement export/import to XLS for:

    http://www.mytaskhelper.com

    that’s why I’m very interested in this plugin.

    I tried Chris’s solution, with fixed plugin for rails 2.1 - it works.

    But I see some problems with that Java library, I show some strange errors in log, especially when I try to stop server.

    Anyone saw this?

  • 4 days ago Jan said

    Igor, I can indeed see it doesn’t work for Rails 2.1, I’ll look into making it work for both 2.1 and 2.2 in a nice way. I haven’t seen the errors you speak of, can you post these?

  • 4 days ago Igor said

    Hi, Jan

    I used this version http://github.com/thechrisoshow/railsxls/tree/2.1

    and it worked, but I noticed some errors - I can’t copy and reproduce it now, because my functionality very changed- that errors was in Java lib, and it looks like memory lick.

    Thank you for making it works with 2.1 - I’m looking forward to try it.

    As I said I’m going to use ‘XLS Export/Import’ for online database MyTaskHelper.com

    And that’s why I need to know what is a advantages of using RailsXLS instead of parseexcel and other plugins.

    I see only difference in “rails way” with using view templates, which generates files. And that’s great.

    It’s important to know, because I’m going to operate with a lot of data.

    Thanks, Sincerely, Igor

  • about 1 hour ago Mark said

    Usefull post! Thanks for sharing!

Leave a Reply