有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
有没有人碰巧知道,如果有一个令牌,我可以添加到我的csv的某个字段,这样Excel就不会试图将它转换为日期?
我试图从我的应用程序中编写一个.csv文件,其中一个值碰巧看起来足够像一个日期,Excel会自动将它从文本转换为日期。我曾尝试将所有文本字段(包括看起来像日期的文本字段)放在双引号内,但没有效果。
当前回答
2018
唯一适合我的解决方案(也没有修改CSV)。
Excel 2010:
创建新工作簿 数据>从文本>选择您的CSV文件 在弹出框中选择“Delimited”单选按钮,然后点击“Next >” 分隔符复选框:只勾选“逗号”,取消勾选其他选项,然后单击“下一步>” 在“数据预览”中,滚动到最右,然后按住shift并单击最后一列(这将选中所有列)。现在在“列数据格式”中选择单选按钮“文本”,然后单击“完成”。
Excel office365:(客户端版本)
创建新工作簿 数据>从文本/CSV >选择您的CSV文件 数据类型检测>不检测
注意:Excel office365 (web版本),当我写这篇文章时,你将无法做到这一点。
其他回答
SELECT CONCAT('\'',NOW(),'\''), firstname, lastname
FROM your_table
INTO OUTFILE 'export.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
警告:Excel '07(至少)有一个(另一个)错误:如果字段的内容中有逗号,它不会正确解析="field, contents",而是将逗号后的所有内容放入下面的字段中,而不管引号是什么。
我发现唯一有效的解决方法是当字段内容包含逗号时消除=。
这可能意味着有些字段不可能在Excel中完全“正确”地表示,但到目前为止,我相信没有人会感到太惊讶。
另一种方法:
将要更改的列的格式转换为“Text”。选择要保留的所有单元格,复制。在不取消这些列的情况下,单击“编辑>粘贴特殊> As值”
保存为CSV。请注意,这必须是您对文件所做的最后一件事,因为当您重新打开它时,它将自己格式化为日期,因为单元格格式不能保存在CSV文件中。
(excel2007及以上版本)
如何强制excel不“检测”日期格式而不编辑源文件
:
将文件重命名为.txt 如果你不能这样做,而不是直接在excel中打开CSV文件,创建一个新的工作簿,然后转到 数据>从文本中获取外部数据>并选择您的CSV。
无论哪种方式,你都会看到导入选项,只需选择每个包含日期的列,并告诉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? .