How to arrange the field in excel by using to_csv

Hi all,

i am exporting the file by using to_csv, but its coming as unordered

rather than my expected output, if i check in the database, its working
fine, but in the excel sheet it shows in the unordered,so please let me
knoe, how to show orderly in the excel sheet by using to_csv.

@report_data=EdiaUserContact.find_by_sql(
“SELECT ‘’ as ‘Title’,
fname as ‘First Name’,’’ as ‘Middle Name’,
lname as ‘Last Name’,’’ as ‘Suffix’,m.member_company_name as
‘Company’ ,
department as ‘Department’,title as ‘Job Title’, addr1 as
‘Business Street’,…”)

respond_to do |wants|
wants.csv do
render :text => @report_data.to_csv
response.headers[‘Content-Type’] = ‘text/csv; charset=iso-8859-1;
header=present’
response.headers[‘Content-Disposition’] = “attachment;
filename=contacts.csv”
end
end

please let me know as soon as possible, thanks in advance

On 10/16/07, Vidya V. [email protected] wrote:

fname as ‘First Name’,‘’ as ‘Middle Name’,
response.headers[‘Content-Disposition’] = “attachment;
filename=contacts.csv”
end
end

please let me know as soon as possible, thanks in advance

Errr… if you want the data sorted, you have to sort it - it’s not
going to magically be in the order you want! Throw an ORDER BY clause
in your SQL.

hi,

Thanks for your Reply, but i already written order by in query itself,
if i check in database it had come as right, but in the excel sheet it
came as unordered, i don’t know why? please let me know, if anybodies
knows.

On 17 Oct 2007, at 05:37, Vidya V. wrote:

hi,

Thanks for your Reply, but i already written order by in query
itself,
if i check in database it had come as right, but in the excel sheet it
came as unordered, i don’t know why? please let me know, if anybodies
knows.

Are you talking about the ordering of the rows or of the columns?

Fred

hi, thanks for ur reply, i am talking about both colums and rows,

see, how the output came in my excel sheet,

Job Title Suffix First Name Company Title Middle Name
dfjh HEGF OddWorlders fghfdgh
yyy dfgdfg OddWorlders

but my expected output is

Title First Name Middle Name Last Name Suffix Company Job Title
Business Street
fghfdgh HEGF OddWorlders dfjh
dfgdfg OddWorlders yyy

like this i want as how i specified the order in the query.

my qurey is

@report_data=EdiaUserContact.find_by_sql(
“SELECT ‘’ as ‘Title’,
fname as ‘First Name’,’’ as ‘Middle Name’,
lname as ‘Last Name’,’’ as ‘Suffix’,m.member_company_name as
‘Company’ ,
department as ‘Department’,title as ‘Job Title’, addr1 as
‘Business Street’
FROM edia_user_contacts, edia_members m, edia_countries c
WHERE m.id=’#{session[:member_id]}’ and user_member_id in (select
partner_id from
edia_member_partners where company_id = ‘#{session[:member_id]}’) and
c.id=address_country”)

sorry, i couldn’t understand clearly, could you please can you explain
little bit clear?

On 17 Oct 2007, at 10:20, Vidya V. wrote:

but my expected output is

Title First Name Middle Name Last Name Suffix Company Job Title
Business Street
fghfdgh HEGF OddWorlders dfjh
dfgdfg OddWorlders yyy

Attributes of an AR object don’t have an order, so you need to
specify it explicity when you call to_csv. As per the docs for
to_csvm you can do this with the :order or :only options

Fred

On 17 Oct 2007, at 10:42, Vidya V. wrote:

sorry, i couldn’t understand clearly, could you please can you explain
little bit clear?

Read the docs for to_csv
http://svn.integralserver.com/plugins/to_csv/README

Hi,

Any body, please let me know, where i should set my options as order
on the above coding?

hi,

i read the doc file, but i don’t where i set the order in the coding,
could you please can you tell this, how to do? where i should set the
order.

respond_to do |wants|
wants.csv do
render :text => @report_data.to_csv
response.headers[‘Content-Type’] = ‘text/csv; charset=iso-8859-1;
header=present’
response.headers[‘Content-Disposition’] = “attachment;
filename=contacts.csv”
end
end

Again… read the docs.

http://svn.integralserver.com/plugins/to_csv/README

:order - Array of columns in explicit order

so

@foo.to_csv(:order=>[:title:, :first_name, :middle_name,
:last_name,:suffix, :company, :job_title, :business, :street])

To be successful with Rails, learn how to not only read but interpret
the
documentation. All of this code is open source so even if the docs
aren’t
clear, you should be able to open the source code and see what it’s
doing.

A wise developer once said that you shouldn’t use any code unless you
understand how it works.

Hi Brian H.

Thanks for you advise, actually i have opened the opend source code
also, but still my expected anwser is not coming, i have passed the
parameters like as you have mentioned on the above, but while running no
reflection in the code, the same output only coming