clonecq
clonecq

Reputation: 36

Incorrect characters in first cell of CSV output file

When database is exported in CSV format, I get this as the first column name: ÔªøId. The output is the same on different computers and operating systems. The rest of the data is correct. The database is created in ActivaAdmin, RoR. What could cause this? no need to explain that I need Id as a column name.

Upvotes: 0

Views: 357

Answers (2)

Danny_ds
Danny_ds

Reputation: 11406

This might be a UTF-8 BOM (Byte Order Mark), (although strictly speaking there is no need for a byte order in UTF-8).

While the characters you show are different, there are exactly 3 bytes at the beginning of your file (and maybe these are converted with copy/paste or reading of the file).

Upvotes: 1

Nitin Srivastava
Nitin Srivastava

Reputation: 1424

Use :type => 'text/csv; charset=<charset>; header=present' in send_data method.

Like, Assuming you've customer model and need to export all customers then

  def export_customers
    @customers = Customer.all
    c = CSV.generate_line(["S.No", "Name", "About", "Phone", "Address", "City", "State", "Country"], :col_sep => ', ', :row_sep => "\r\n", :quote_char => '"')
    @customers.each_with_index do |customer, index|
      c += CSV.generate_line([index + 1, customer.name, customer.about, customer.phone, customer.address, customer.city, customer.state, customer.country], :col_sep => ', ', :row_sep => "\r\n", :quote_char => '"')
    end

    send_data(c, :type => 'text/csv; charset=<charset>; header=present', :filename => 'customers.csv')
end

Upvotes: 0

Related Questions