Is it possible to force Excel recognize UTF-8 CSV files automatically?

后端 未结 27 2047
醉梦人生
醉梦人生 2020-11-21 22:27

I\'m developing a part of an application that\'s responsible for exporting some data into CSV files. The application always uses UTF-8 because of its multilingual nature at

27条回答
  •  野的像风
    2020-11-21 23:08

    hi i'm using ruby on rails for csv generation. In our application we plan to go for the multi language(I18n) and we faced an issue while viewing I18n content in the CSV file of windows excel.

    Was fine with Linux (Ubuntu) and mac.

    We identified that windows excel need to be imported the data again to view the actual data. While import we will get more options to choose character set.

    But this can’t be educated for each and every user, so solution we looking for is to open just by double click.

    Then we identified the way of showing data by open mode and bom in windows excel with the help of aghuddleston gist. Added at reference.

    Example I18n content

    In Mac and Linux

    Swedish : Förnamn English : First name

    In Windows

    Swedish : Förnamn English : First name

    def user_information_report(report_file_path, user_id)
        user = User.find(user_id)
        I18n.locale = user.current_lang
        open_mode = "w+:UTF-16LE:UTF-8"
        bom = "\xEF\xBB\xBF"
        body user, open_mode, bom
      end
    
    def headers
        headers = [
            "ID", "SDN ID",
            I18n.t('sys_first_name'), I18n.t('sys_last_name'), I18n.t('sys_dob'),
            I18n.t('sys_gender'), I18n.t('sys_email'), I18n.t('sys_address'),
            I18n.t('sys_city'), I18n.t('sys_state'), I18n.t('sys_zip'),
            I18n.t('sys_phone_number')
        ]
      end
    
    def body tenant, open_mode, bom
        File.open(report_file_path, open_mode) do |f|
          csv_file = CSV.generate(col_sep: "\t") do |csv|
            csv << headers
            tenant.patients.find_each(batch_size: 10) do |patient|
              csv <<  [
                  patient.id, patient.patientid,
                  patient.first_name, patient.last_name, "#{patient.dob}",
                  "#{translate_gender(patient.gender)}", patient.email, "#{patient.address_1.to_s} #{patient.address_2.to_s}",
                  "#{patient.city}", "#{patient.state}",  "#{patient.zip}",
                  "#{patient.phone_number}"
              ]
            end
          end
          f.write bom
          f.write(csv_file)
        end
      end
    

    Important things to note here is open mode and bom

    open_mode = "w+:UTF-16LE:UTF-8"

    bom = "\xEF\xBB\xBF"

    Before writing the CSV insert BOM

    f.write bom

    f.write(csv_file)

    Windows and Mac

    File can be opened directly by double clicking.

    Linux (ubuntu)

    While opening a file ask for the separator options -> choose “TAB”

提交回复
热议问题