# Display problem with to\_csv

**URL:** <https://www.ruby-forum.com/t/display-problem-with-to-csv/116924>\
**Category:** Rails\
**Created:** [October 11, 2007, 8:51am UTC](https://www.ruby-forum.com/t/display-problem-with-to-csv/116924 "2007-10-11T08:51:13Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![vidyagayathri](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/v/ba9def/32.png) [@vidyagayathri](https://www.ruby-forum.com/u/vidyagayathri)\
**Post date:** [October 11, 2007, 8:51am UTC](https://www.ruby-forum.com/t/display-problem-with-to-csv/116924/1 "2007-10-11T08:51:13Z")

</div>

Hi all,

I need to export the document as csv file, i have exported  
successfully, but the problem is not displayed as per my format, in csv  
file the format is suffuled, i want the orderwise as how i have  
mentioned in my query, please help me to come out in the order wise.  
My coding 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’,  
`addr2` as ‘Business Street1’,`addr3` as ‘Business Street2’,`addr4` as  
‘Business Street3’,  
`address_city` as ‘Business City’,`address_state` as ‘Business  
State’,`address_postalcode`  
as ‘Business Postal Code’,c.c\_name as ‘Business Country’ ,’’ as ‘Home  
Street’,  
‘’ as ‘Home Street 2’,’’ as ‘Home Street 3’,’’ as ‘Home City’,  
‘’ as ‘Home State’,’’ as ‘Home Postal Code’,’’ as ‘Home Country’,  
‘’ as ‘Other Street’,’’ as ‘Other Street 2’,’’ as ‘Other Street 3’,’’  
as ‘Other City’,  
‘’ as ‘Other State’,’’ as ‘Other Postal Code’,’’ as ‘Other Country’,’’  
as ‘Assistants Phone’,  
`phone_fax` as ‘Business Fax’,`phone_work` as ‘Business Phone’,  
`phone_mobile` as ‘Business Phone 2’,’’ as ‘Callback’,’’ as ‘Car  
Phone’,  
`phone_work` as ‘Company Main Phone’,’’ as ‘Home Fax’,  
`phone_home`as ‘Home Phone’,  
‘’ as ‘Home Phone 2’,’’ as ‘ISDN’,`phone_mobile` as ‘Mobile Phone’,’’  
as ‘Other Fax’,  
‘’ as ‘Other Phone’,’’ as ‘Pager’,’’ as ‘Primary Phone’,’’ as ‘Radio  
Phone’,’’ as ‘TTY/TDD Phone’,  
‘’ as ‘Telex’,’’ as ‘Account’,’’ as ‘Anniversary’,’’ as ‘Assistants  
Name’,’’ as ‘Billing Information’,  
‘’ as ‘Birthday’,’’ as ‘Categories’,’’ as ‘Children’, `email1` as  
‘E-mail Address’,`email2` as ‘E-mail Display Name’,  
‘’ as ‘E-mail 2 Address’,’’ as ‘E-mail 2 Display Name’,’’ as ‘E-mail 3  
Address’,’’ as ‘E-mail 3 Display Name’,  
‘’ as ‘Gender’,’’ as ‘Government ID Number’,’’ as ‘Hobby’,’’ as  
‘Initials’,’’ as ‘Keywords’,  
‘’ as ‘Language’,’’ as ‘Location’,’’ as ‘Mileage’,’’ as ‘Notes’,’’ as  
‘Office Location’,  
‘’ as ‘Organizational ID Number’,’’ as ‘PO Box’,’’ as ‘Private’,’’ as  
‘Profession’,  
‘’ as ‘Referred By’,’’ as ‘Spouse’,’’ as ‘User 1’,’’ as ‘User 2’,’’ as  
‘User 3’,  
‘’ as ‘User 4’,’’ as’Web Page’  
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`”)

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

```auto
expected output in excel file should come in the order wise as how i
have mentioned in the query.

  but at present, its coming like in a suffled order, like this in excel
file

Home Phone E-mail Display Name Office Location Language Home Street
Notes Home Phone 2 Business Phone Company Main Phone Business State
User 4 Other State Title User 3 Business Phone 2 Account Other
Street Private Profession Radio Phone User 2 Referred By User 1
Location Home State Pager Middle Name Business City Keywords
Business Country Organizational ID Number E-mail 3 Display Name
Department Home Street 3 Home Postal Code Gender Home Street 2
Mileage Business Street Other Street 3 Job Title Home Fax Web Page
Business Fax Other Street 2 Business Street3 Other Country E-mail
Address Business Street2 Home City Other Fax ISDN First Name
Anniversary Home Country PO Box TTY/TDD Phone Business Street1
E-mail 3 Address Car Phone Other Postal Code Government ID Number
E-mail 2 Address Suffix Assistants Phone Spouse Billing Information
Business Postal Code Mobile Phone Company Other City Callback Telex
Birthday Other Phone Primary Phone E-mail 2 Display Name Children
Last Name Assistants Name Categories Hobby Initials

   if anyone knows how to do the order wise, please let me know as soon
as possible. Thanks in Advance.
```
