FasterCSV 和非拉丁字符
我最近编写了代码,可以帮助我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不习惯使用中文字符,但您可以尝试在
generate
调用中将:encoding
选项添加到 'u' (UTF-8):旁注,我建议使用 named_scopes 而不是这样写:
祝你好运!
I'm not used to work with chinese characters, but you can try adding the
:encoding
option to 'u' (UTF-8) on thegenerate
call:As a side note, I'd recommend using named_scopes instead of writing this:
Good luck!