有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
当前回答
另一种方法:
将要更改的列的格式转换为“Text”。选择要保留的所有单元格,复制。在不取消这些列的情况下,单击“编辑>粘贴特殊> As值”
保存为CSV。请注意,这必须是您对文件所做的最后一件事,因为当您重新打开它时,它将自己格式化为日期,因为单元格格式不能保存在CSV文件中。
其他回答
无需修改您的csv文件,您可以:
将excel格式单元格选项更改为“text” 然后使用“文本导入向导”定义csv单元格。 一旦导入,删除该数据 然后粘贴为纯文本
Excel将正确格式化和分离您的CSV单元格为文本格式,忽略自动日期格式。
有点愚蠢的工作,但它比修改csv数据之前导入。安迪·贝尔德(Andy Baird)和理查德(Richard)回避了这种方法,但遗漏了几个重要步骤。
我有一个类似的问题,这是解决方案,帮助我无需编辑csv文件内容:
如果你可以灵活地将文件命名为“。csv”以外的东西,你可以用“。txt”扩展名来命名它,比如“Myfile.txt”或“Myfile.csv.txt”。然后,当你在Excel中打开它(不是通过拖放,而是使用文件->打开或最近使用的文件列表),Excel将为你提供一个“文本导入向导”。
在向导的第一页中,为文件类型选择“Delimited”。
在向导的第二页中,选择“,”作为分隔符,如果用引号括起值,还可以选择文本限定符
在第三页,分别选择每一列,并为每一列分配类型为“文本”而不是“常规”,以防止Excel弄乱你的数据。
希望这能帮助你或有类似问题的人!
如果在字段的开头放置一个倒置逗号,它将被解释为文本。
例子: 25/12/2008变成了25/12/2008
您还可以在导入时选择字段类型。
在双引号中添加空格前缀解决了这个问题!!
我在csv文件的一个列中有“7/8”这样的数据,MS-Excel将其转换为“07-Aug”。但是使用“LibreOffice Calc”就没有问题了。
为了解决这个问题,我只是给空格字符加上前缀(在7之前添加空格),比如“7/8”,这对我来说很有效。这是为Excel-2007测试的。
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? .