有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?

我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。


当前回答

我有一个类似的问题,这是解决方案,帮助我无需编辑csv文件内容:

如果你可以灵活地将文件命名为“。csv”以外的东西,你可以用“。txt”扩展名来命名它,比如“Myfile.txt”或“Myfile.csv.txt”。然后,当你在Excel中打开它(不是通过拖放,而是使用文件->打开或最近使用的文件列表),Excel将为你提供一个“文本导入向导”。

在向导的第一页中,为文件类型选择“Delimited”。

在向导的第二页中,选择“,”作为分隔符,如果用引号括起值,还可以选择文本限定符

在第三页,分别选择每一列,并为每一列分配类型为“文本”而不是“常规”,以防止Excel弄乱你的数据。

希望这能帮助你或有类似问题的人!

其他回答

如果在字段的开头放置一个倒置逗号,它将被解释为文本。

例子: 25/12/2008变成了25/12/2008

您还可以在导入时选择字段类型。

这周我刚刚遇到了这个惯例,它似乎是一个很好的方法,但我在任何地方都找不到它。有人熟悉吗?你能举出出处吗?我没有找了几个小时,但我希望有人会认识到这种方法。

例1:=("012345678905")显示为012345678905

例2:=("1954-12-12")显示为1954-12-12,而不是12/12/1954。

我发现在双引号前加一个'='就能达到你想要的效果。它强制数据为文本。

如。=“2008-10-03”=“文本”

编辑(根据其他帖子):由于Jeffiekins指出的Excel 2007漏洞,应该使用Andrew提出的解决方案:"=""2008-10-03""" "

在我的案例中,使用R生成的csv文件中的“Sept8”被Excel 2013转换为“8-Sept”。该问题通过使用xlsx包中的write.xlsx2()函数生成xlsx格式的输出文件来解决,该输出文件可以由Excel加载,而无需进行不必要的转换。因此,如果给你一个csv文件,你可以尝试将它加载到R中,并使用write.xlsx2()函数将其转换为xlsx。

None of the solutions offered here is a good solution. It may work for individual cases, but only if you're in control of the final display. Take my example: my work produces list of products they sell to retail. This is in CSV format and contain part-codes, some of them start with zero's, set by manufacturers (not under our control). Take away the leading zeroes and you may actually match another product. Retail customers want the list in CSV format because of back-end processing programs, that are also out of our control and different per customer, so we cannot change the format of the CSV files. No prefixed'=', nor added tabs. The data in the raw CSV files is correct; it's when customers open those files in Excel the problems start. And many customers are not really computer savvy. They can just about open and save an email attachment. We are thinking of providing the data in two slightly different formats: one as Excel Friendly (using the options suggested above by adding a TAB, the other one as the 'master'. But this may be wishful thinking as some customers will not understand why we need to do this. Meanwhile we continue to keep explaining why they sometimes see 'wrong' data in their spreadsheets. Until Microsoft makes a proper change I see no proper resolution to this, as long as one has no control over how end-users use the files.