In this post I am not going to brief you on how to export any table as Excel Sheet and CSV in ruby on rails.
It can be found in this link--> Exporting as CSV and EXcel Sheet
I will address the problem which I faced while using it.
There was no exception and even the download link was working fine.
However when I downloaded the excel file and opened it, the data in the files was not the content of the table . Rather the excel sheet was rendering the lists of objects like this-
Note:- It may happen that you are able to export an excel sheet without this solution also. But if you are not try this !!
Replace this:
format.csv { send_data @colleagues.to_csv }
format.xls { send_data @colleagues.to_csv(col_sep: "\t") }
It can be found in this link--> Exporting as CSV and EXcel Sheet
I will address the problem which I faced while using it.
Problem
I followed all the steps mentioned in the link and everything seems to be working fine.There was no exception and even the download link was working fine.
However when I downloaded the excel file and opened it, the data in the files was not the content of the table . Rather the excel sheet was rendering the lists of objects like this-
#<Colleague:0x35e1e20>, #<Colleague:0x35e19d0> |
Solution
In controller, instead of using @colleagues i.e the instance variable use the model name instead like this.
Note:- It may happen that you are able to export an excel sheet without this solution also. But if you are not try this !!
Replace this:
format.csv { send_data @colleagues.to_csv }
format.xls { send_data @colleagues.to_csv(col_sep: "\t") }
With
format.csv { send_data Colleague.to_csv }
format.xls { send_data Colleague.to_csv(col_sep: "\t") }
Some tips:
format.xls { send_data Colleague.to_csv(col_sep: "\t") }
Some tips:
- Make sure to restart the server after changing the config files.
- Gems like spreadsheet are also available which can be used for this same purpose.
Hi nikita,
ReplyDeleteBut what if i want to get filtered columns like " @eb= Register.find(:all,:select => "filo_file_name,filo_content_type") " then using model name will not be working.
Yes, This will only work when you want to export each and every column of your model
ReplyDelete