Export to Excel in Rails 3 without a Gem
Creating Demo project
rails new xcel_demo –d mysql
Creating a User model
rails g scaffold User name:string email:string content:string
Add a MIME type in \config\initializers\mime_type.rb
Mime::Type.register 'application/vnd.ms-excel', :xls
Add a xls format output to the method in the Controller
def index
@users = User.all
respond_to do |format|
format.html # index.html.erb
format.xls
format.xml { render :xml => @users }
end
end
Creating the view for the XLS
Now create a view to render the xls file. Here as we have modified the index method we will create a view index.xls.erb for the same in the views for Users
<h1>Listing users</h1>
<table>
<tr>
<th>Name</th>
<th>City</th>
<th>Gender</th>
<th>Phone</th>
<th>Address</th>
<th></th>
<th></th>
<th></th>
</tr>
<% @users.each do |user| %>
<tr>
<td><%= user.name %></td>
<td><%= user.city %></td>
<td><%= user.gender %></td>
<td><%= user.phone %></td>
<td><%= user.address %></td>
</tr>
<% end %>
</table>
<br />
The text in the <th> tags are the headers and <td> tags are the data for the specified columns
Adding the link to export excel on the Index page
<%= link_to 'Export XLS', url_for(:format => 'xls') %>
This will allow the data on index page to be exported in Excel Format.
Great thanks!
ReplyDeleteHow to export special symbols, like "€"?
ReplyDeleteITS SHOWING SOME ERROR LIKE THIS WHAT TO DO HELP ME
ReplyDeleteMissing template users/index, application/index with {:locale=>[:en], :formats=>[:xls], :handlers=>[:erb, :builder, :coffee]}. Searched in: * "C:/Users/Ranjith/Desktop/aa/app/views"
Hi have you created the index.xls.erb in the views folder ??
ReplyDeletedo you have any idea why my html is not parsed in the xls file? I see the html-tags line by line!
ReplyDeletehave you followed all the steps ?? MIME::TYPE and all ?? please follow all the steps , if the issue occurs again , paste me the sample code or mail it to me :) ashish.upadhyaye@gmail.com
ReplyDeleteI checked, I'm using the exact same code. I'm running rails 3.1 on opensuse. Could this be a problem?
DeleteThanks!
I have not tried out this on Rails 3.1 though, will update the post if I do it in a while, try out any of the Gems available they will give you more authority over excel generation .
DeleteYou may use this https://www.ruby-toolbox.com/projects/excel_rails
Thanks, you are good) Any other articles didn't help me!
ReplyDeleteHow can I force Excel export to show All Borders?
ReplyDeleteIts really awesome. Thanks a lot
ReplyDeleteHi Ashish it is really awesome and helpful but could you please tell me how can we make it uneditable.
ReplyDeleteHow to change the filename of this Excel file?
ReplyDeleteThanks
How can you use special characters or umlauts? Chars like "§" are not working for me.
ReplyDeleteExcelent, realy simple!
ReplyDeleteHi, i have tried generating excel file, but can you help me to render image to excel field.
ReplyDeleteThanks in advance.
Thank you very much bayya :)
ReplyDeletegreat post - is there a gemless way to get an excel import to work?
ReplyDeleteReally nice blog post.provided a help full information.I hope that you will post more updates like this
ReplyDeleteRuby on Rails Online Training Hyderabad
Fon Perde Modelleri
ReplyDeleteSMS ONAY
Mobil ödeme bozdurma
nft nasıl alınır
ankara evden eve nakliyat
Trafik Sigortasi
dedektör
Websitesi Kurmak
Ask romanlari
smm panel
ReplyDeletesmm panel
Ä°S Ä°LANLARÄ°
instagram takipçi satın al
hirdavatciburada.com
beyazesyateknikservisi.com.tr
Servis
jeton hilesi
Good content. You write beautiful things.
ReplyDeletekorsan taksi
sportsbet
mrbahis
vbet
mrbahis
sportsbet
hacklink
hacklink
vbet
dijital kartvizit
ReplyDeletereferans kimliÄŸi nedir
binance referans kodu
referans kimliÄŸi nedir
bitcoin nasıl alınır
resimli magnet
SN7