Ruby:如何生成具有Excel友好编码的CSV文件 [英] Ruby: How to generate CSV files that has Excel-friendly encoding

查看:234
本文介绍了Ruby:如何生成具有Excel友好编码的CSV文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成的CSV文件在生成后需要在Excel中打开和查看。看起来Excel需要不同于UTF-8的编码。



这是我的配置和代码:

  csv_config = {col_sep:;,
row_sep:\\\

encoding:Encoding :: UTF_8
}

csv_string = CSV.generate(csv_config)do | csv |
csv<< [文本a,文本b,文本æ,文本ø,文本å]
结束

在Excel中打开此文件时,特殊字符无法正确显示:

 文本任何想法如何确保正确的编码?

/ p>

解决方案

您可以尝试设置 csv_config ,如下所示:

  csv_config = {
col_sep:;,
row_sep:\\\

编码:'ISO-8859-1'
}

我有同样的问题,编码固定。


I am generating CSV files that needs to be opened and reviewed in Excel once they have been generated. It seems that Excel requires a different encoding than UTF-8.

Here is my config and generation code:

csv_config = {col_sep: ";", 
              row_sep: "\n", 
              encoding: Encoding::UTF_8
             }

csv_string = CSV.generate(csv_config) do |csv|
  csv << ["Text a", "Text b", "Text æ", "Text ø", "Text å"]
end

When opening this in Excel, the special characters are not being displayed properly:

Text a  Text b  Text æ Text ø Text å

Any idea how to ensure proper encoding?

解决方案

You can try set your csv_config like the following:

csv_config = {
              col_sep: ";", 
              row_sep: "\n", 
              encoding: 'ISO-8859-1'
             }

I had the same issue and that encoding fixed.

这篇关于Ruby:如何生成具有Excel友好编码的CSV文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆