自动为 C++ 中的 Excel 生成区域设置 csv 文件
我想生成一个包含数字的 csv 文件,并且希望 Excel 自动读取该文件。
但是,Excel 根据区域设置使用不同的 csv 格式。例如:
英语:
- 文本分隔符: ,
- 小数分隔符: 。
西班牙语:
- 文本分隔符:;
- 小数分隔符: ,
是否可以自动检测 C++ 中的这些设置,以便我可以根据它们生成文件?
我知道我可以使用区域设置对象获取小数分隔符,但我不知道如何获取文本分隔符。
这个问题与: CSV 实际上是......分号分隔值。但是,我不想读取该文件,而是生成它,并且我想自动检测设置。
I want to generate a csv file containing numbers and I want Excel to read that file automatically.
However, Excel uses a different csv format depending on the locale settings. For example:
English:
- Text separator: ,
- Decimal separator: .
Spanish:
- Text separator: ;
- Decimal separator: ,
Is it possible to automatically detect those settings in C++ so I can generate the file according to them?
I know I can obtain the decimal separator using a locale object, but I do not know how to obtain the text separator.
This question is related to:
CSV is actually … Semicolon Separated Values . However, I do not want to read the file but to generate it and I want to automatically detect the settings.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我不认为标准 C++ 有“文本分隔符”的概念,并且这是 Windows 特定的。因此,您可以将 GetLocaleInfo 与 LOCALE_SLIST 标志一起使用。
I don't think that standard C++ has a notion of "text separator", and that this is Windows-specific. You could thus use GetLocaleInfo with the LOCALE_SLIST flag.
可以,但不是 100% 确定(但非常接近)。可以通过分析文件(或合适的样本)并确定每个记录中每个字符的频率来检测文本分隔符。每个记录的文本分隔符应该相同(或几乎相同)。它的频率将比每条记录中的字段数少一。
好的分析器不会计算受双引号保护的字符,因为这些字符是嵌入的而不是分隔符(即,不要计算“Boston, MA 02066”中的逗号,因为它受双引号保护)。
确定文本分隔符后即可确定小数分隔符。只需查找相邻的数字并查看分隔它们的内容(这不是之前确定的文本分隔符)。
You can, but not with 100% certainty (but very close to it). Detecting the text separator can be done by analyzing the file (or a decent sample) and determining the frequencies of each character in each record. The text separator should be the same (or nearly the same) for every record. It's frequency will be one less than the number of fields in each record.
A good analyzer will not count characters that are protected by double quotes, as these are embedded and not separators (ie, don't count the comma in "Boston, MA 02066", as it is protected by the double quotes).
Decimal separators can be determined after you've determined the text separator. Just look for adjacent numbers and see what's separating them (that's not the previously determined text separator).
使用 CultureInfo.TextInfo.ListSeparator。
Use CultureInfo.TextInfo.ListSeparator.