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

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


当前回答

CSV -用逗号分隔的值。只需通过文本编辑器创建/编辑,而不是xls/xlsx/exel。 在编辑时,您可以设置所需的格式的日期,它必须保持完整。 这是假设同样的文件将被以编程方式处理。

其他回答

对于同样的问题,我所做的是在每个csv值之前添加以下内容: “=”“” 在Excel中打开文件之前,在每个CSV值后加上双引号。以以下值为例:

012345,00198475

在Excel中打开之前,这些应该更改为:

"="""012345","="""00198475"

这样做之后,每个单元格值在Excel中都显示为公式,因此不会格式化为数字、日期等。例如,012345的值显示为:

="012345"

根据Jarod的解决方案和Jeffiekins提出的问题,你可以进行修改

"May 16, 2011"

to

"=""May 16, 2011"""

I know this is an old question, but the problem is not going away soon. CSV files are easy to generate from most programming languages, rather small, human-readable in a crunch with a plain text editor, and ubiquitous. The problem is not only with dates in text fields, but anything numeric also gets converted from text to numbers. A couple of examples where this is problematic: ZIP/postal codes telephone numbers government ID numbers which sometimes can start with one or more zeroes (0), which get thrown away when converted to numeric. Or the value contains characters that can be confused with mathematical operators (as in dates: /, -). Two cases that I can think of that the "prepending =" solution, as mentioned previously, might not be ideal is where the file might be imported into a program other than MS Excel (MS Word's Mail Merge function comes to mind), where human-readability might be important. My hack to work around this If one pre/appends a non-numeric and/or non-date character in the value, the value will be recognized as text and not converted. A non-printing character would be good as it will not alter the displayed value. However, the plain old space character (\s, ASCII 32) doesn't work for this as it gets chopped off by Excel and then the value still gets converted. But there are various other printing and non-printing space characters that will work well. The easiest however is to append (add after) the simple tab character (\t, ASCII 9). Benefits of this approach: Available from keyboard or with an easy-to-remember ASCII code (9), It doesn't bother the importation, Normally does not bother Mail Merge results (depending on the template layout - but normally it just adds a wide space at the end of a line). (If this is however a problem, look at other characters e.g. the zero-width space (ZWSP, Unicode U+200B) is not a big hindrance when viewing the CSV in Notepad (etc), and could be removed by find/replace in Excel (or Notepad etc). You don't need to import the CSV, but can simply double-click to open the CSV in Excel. If there's a reason you don't want to use the tab, look in an Unicode table for something else suitable. Another option might be to generate XML files, for which a certain format also is accepted for import by newer MS Excel versions, and which allows a lot more options similar to .XLS format, but I don't have experience with this. So there are various options. Depending on your requirements/application, one might be better than another. Addition It needs to be said that newer versions (2013+) of MS Excel don't open the CSV in spreadsheet format any more - one more speedbump in one's workflow making Excel less useful... At least, instructions exist for getting around it. See e.g. this Stackoverflow: How to correctly display .csv files within Excel 2013? .

(假设Excel 2003…)

当使用文本到列向导时,在步骤3中,您可以为每个列指定数据类型。单击预览中的列,将行为不端的列从“常规”更改为“文本”。

这个问题仍然存在于Mac Office 2011和Office 2013,我无法阻止它发生。这似乎是很基本的事情。

在我的例子中,我有诸如“1 - 2”和“7 - 12”的值,在CSV中正确地用倒逗号括起来,这将自动转换为excel中的日期,如果您尝试随后将其转换为纯文本,您将得到日期的数字表示形式,如43768。此外,它将条形码和EAN编号中的大数字重新格式化为123E+数字,再次无法转换回来。

我发现谷歌驱动器的谷歌表不能将数字转换为日期。条形码中每3个字符有一个逗号,但这些很容易被删除。它处理csv非常好,特别是在处理MAC / Windows csv时。

说不定还能救人。