FasterCSV 和非拉丁字符

发布于 2024-10-01 13:11:13 字数 972 浏览 1 评论 0原文

我最近编写了代码,可以帮助我使用 FasterCSV 和 Rails 将 SQL 数据库导出为 CSV。但是我的数据库的某些部分包含繁体中文字符。当我导出它时,我得到了??????作为 CSV 文件中的输出。 Iv 已经尝试更改 $KCODE = 'u' 以便 FasterCSV 使用 UTF-8 对 CSV 文件进行编码,但没有成功。转换编码的 iconv 也给了我奇怪的结果。这是源代码:

def csv
@lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

csv_string = FasterCSV.generate do |csv|
  csv << [<bold> "Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]

  @lists.each do |project|
    csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
  end
end

filename = Time.now.strftime("%Y%m%d") + ".csv"
send_data(csv_string,
  :type => 'text/csv; charset=utf-8; header=present',
  :filename => filename)

结束

谢谢,

Iv recently written code that will help me export an SQL database into CSV using FasterCSV with rails. However some parts of my database contain Traditional Chinese Characters. When I export it i'm getting ?????? as the output in the CSV file. Iv already tried changing the $KCODE = 'u' so that FasterCSV uses UTF-8 to encode the CSV file, but no luck. Iconv to convert the encoding is giving me strange results as well. Here is the source code:

def csv
@lists = Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

csv_string = FasterCSV.generate do |csv|
  csv << [<bold> "Status","Name","Summary","Description","Creator","Comment","Contact Information","Created Date","Updated Date"]

  @lists.each do |project|
    csv << [project.status, project.name, project.summary, project.description, project.creator, project.statusreason, project.contactinfo, project.created_at, project.updated_at]
  end
end

filename = Time.now.strftime("%Y%m%d") + ".csv"
send_data(csv_string,
  :type => 'text/csv; charset=utf-8; header=present',
  :filename => filename)

end

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-10-08 13:11:13

我不习惯使用中文字符,但您可以尝试在 generate 调用中将 :encoding 选项添加到 'u' (UTF-8)

...
csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
...

:旁注,我建议使用 named_scopes 而不是这样写:

Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

祝你好运!

I'm not used to work with chinese characters, but you can try adding the :encoding option to 'u' (UTF-8) on the generate call:

...
csv_string = FasterCSV.generate(:encoding => 'u') do |csv|
...

As a side note, I'd recommend using named_scopes instead of writing this:

Project.find(:all, :order=> (params[:sort] + ' ' + params[:direction]), :conditions =>  ["name LIKE ?", "%#{params[:selection]}%"])

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文