CSV 导出带有前导零的数字,可在 Excel 中读取
是否有可能影响 ruby CSV 模块在引用字段之前添加等号,以便生成的 csv 在 Excel 中可读并且不会丢失前导零。
当前结果是:
ruby-1.9.2-p0 > CSV.generate_line(["01"], :force_quotes => true)
=> "\"01\"\n"
首选是:
ruby-1.9.2-p0 > CSV.generate_line(["01"], :force_quotes => true)
=> "=\"01\"\n"
我知道可以选择在 Excel 中导入文本文件,但我想避免这种情况,并且只需双击该文件即可将其打开。
Is it possible to influence ruby CSV module to add equal sign before quoted field, so generated csv is readable in excel and does not loose leading zero.
Current result is:
ruby-1.9.2-p0 > CSV.generate_line(["01"], :force_quotes => true)
=> "\"01\"\n"
Preferred would be:
ruby-1.9.2-p0 > CSV.generate_line(["01"], :force_quotes => true)
=> "=\"01\"\n"
I know there is option to import text file in excel, but I would like to avoid this and be able just to double click on the file to open it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
CSV用于生成CSV,而不是Excel文件。因此,您要么需要自己对 CSV 进行猴子修补,但出于您的目的,
Spreadsheet::Excel
可能是更好的选择。这允许您生成本机 Excel 文件,您应该几乎能够直接设置列格式选项以匹配您要查找的内容:http://spreadsheet.rubyforge.org/file.GUIDE.html
CSV is for generating CSV, not Excel files. So you either need to monkey-patch CSV yourself, but probably for your purposes
Spreadsheet::Excel
is a better option. This allows you to generate native Excel files, where you should pretty much be able to set the column formatting options directly to match what you are looking for:http://spreadsheet.rubyforge.org/file.GUIDE.html