有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
当前回答
这个问题仍然存在于Mac Office 2011和Office 2013,我无法阻止它发生。这似乎是很基本的事情。
在我的例子中,我有诸如“1 - 2”和“7 - 12”的值,在CSV中正确地用倒逗号括起来,这将自动转换为excel中的日期,如果您尝试随后将其转换为纯文本,您将得到日期的数字表示形式,如43768。此外,它将条形码和EAN编号中的大数字重新格式化为123E+数字,再次无法转换回来。
我发现谷歌驱动器的谷歌表不能将数字转换为日期。条形码中每3个字符有一个逗号,但这些很容易被删除。它处理csv非常好,特别是在处理MAC / Windows csv时。
说不定还能救人。
其他回答
下面是我们在生成csv文件时使用的简单方法,它确实会稍微改变值,所以并不适用于所有应用程序:
在csv中的所有值前加一个空格
excel将从诸如“1”、“2.3”和“-2.9e4”等数字中剥离该空格,但将保留诸如“01/10/1993”这样的日期和诸如“TRUE”这样的布尔值,从而阻止它们转换为excel的内部数据类型。
它也停止双引号被zapped在读进去,所以一个简单的方法,使文本在csv中保持不变的excel即使是一些文本,如“3.1415”是用双引号包围它,并在整个字符串前加上一个空格,即(使用单引号来显示你会键入什么)“3.1415”。然后在excel中,你总是有原始的字符串,除了它被双引号包围并以空格开头,所以你需要在任何公式中考虑这些。
如果可以更改文件源数据
如果您准备更改原始源CSV文件,另一种选择是更改数据中的“分隔符”,因此如果您的数据是“4/11”(或4-11),Excel将其转换为4/11/2021(英国或11-4-2021美国),那么将“/”或“-”字符更改为其他字符将阻止不想要的Excel日期转换。选项包括:
波浪号(~) +(“+”) 下划线(“_”) 双破折号(“-”) En-dash (Alt 150) Em-dash (Alt 151) (其他角色!)
注意:如果文件要在其他地方使用,则移动到Unicode或其他非ascii/ansi字符可能会使问题复杂化。
因此,'4-11'转换为'4~11'与波浪号将不会被视为日期!
对于大型CSV文件,这没有额外的开销(即:额外的引号/空格/制表符/公式结构),只在文件直接打开时工作(即:双击CSV打开),并避免将列预先格式化为文本或“导入”CSV文件为文本。
如果需要,在记事本(或类似工具)中的搜索/替换可以轻松地转换为/从替代分隔符。
导入原始数据
在新版本的Excel中,您可以导入数据(在其他答案中概述)。 在较旧版本的Excel中,您可以安装“Power Query”插件。该工具还可以导入csv而不进行转换。选择:Power Query选项卡/来自文件/来自文本- csv,然后“Load”以表格形式打开。(您可以从“数据类型检测”选项中选择“不检测数据类型”)。
另一种方法:
将要更改的列的格式转换为“Text”。选择要保留的所有单元格,复制。在不取消这些列的情况下,单击“编辑>粘贴特殊> As值”
保存为CSV。请注意,这必须是您对文件所做的最后一件事,因为当您重新打开它时,它将自己格式化为日期,因为单元格格式不能保存在CSV文件中。
警告:Excel '07(至少)有一个(另一个)错误:如果字段的内容中有逗号,它不会正确解析="field, contents",而是将逗号后的所有内容放入下面的字段中,而不管引号是什么。
我发现唯一有效的解决方法是当字段内容包含逗号时消除=。
这可能意味着有些字段不可能在Excel中完全“正确”地表示,但到目前为止,我相信没有人会感到太惊讶。
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? .