有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,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”以表格形式打开。(您可以从“数据类型检测”选项中选择“不检测数据类型”)。
其他回答
对于同样的问题,我所做的是在每个csv值之前添加以下内容: “=”“” 在Excel中打开文件之前,在每个CSV值后加上双引号。以以下值为例:
012345,00198475
在Excel中打开之前,这些应该更改为:
"="""012345","="""00198475"
这样做之后,每个单元格值在Excel中都显示为公式,因此不会格式化为数字、日期等。例如,012345的值显示为:
="012345"
将表格粘贴到word中。进行搜索/替换并将所有-(破折号)更改为-(双破折号)。复制并粘贴到Excel中。可以为其他符号(/)等做同样的事情。如果需要在Excel中更改回破折号一次,只需将列格式化为文本,然后进行更改。希望这能有所帮助。
如果有人还在寻找答案,下面这句话对我来说非常合适
我输入=("my_value")。 即=("04SEP2009")显示为04SEP2009,而不是09/04/2009
这同样适用于大于15位的整数。他们不再修剪了。
另一种方法:
将要更改的列的格式转换为“Text”。选择要保留的所有单元格,复制。在不取消这些列的情况下,单击“编辑>粘贴特殊> As值”
保存为CSV。请注意,这必须是您对文件所做的最后一件事,因为当您重新打开它时,它将自己格式化为日期,因为单元格格式不能保存在CSV文件中。
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.