CSV 导出带有前导零的数字,可在 Excel 中读取

发布于 2024-12-19 18:31:30 字数 390 浏览 4 评论 0原文

是否有可能影响 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 技术交流群。

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

发布评论

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

评论(1

谁与争疯 2024-12-26 18:31:30

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

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