php 通过php在csv单元格中创建回车
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4025831/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Creating carriage returns in csv cell via php
提问by Collin White
I'm trying to dynamically generate a csv file with some cells that will contain multiple lines, the address field for example will need to be grouped into a single "address" cell instead of address,city,state etc. All is going well and but for the last two days i've tried to insert \r, \r\n, \n, chr(10),chr(13), as well as a carriage return within the code to create the carriage return i'm looking for within the cell. All of these fail, either being literally printed in my csv as "\r" etc or when I do a manual carriage return in the code it generates a new row. I'm using this to create the breaks in my cells but it isn't working
我正在尝试动态生成一个 csv 文件,其中包含一些包含多行的单元格,例如,地址字段需要分组到一个“地址”单元格中,而不是地址、城市、州等。一切进展顺利,并且但是在过去的两天里,我试图在代码中插入 \r、\r\n、\n、chr(10)、chr(13) 以及一个回车来创建回车,我是在细胞内寻找。所有这些都失败了,要么在我的 csv 中字面上打印为“\r”等,要么当我在代码中手动回车时,它会生成一个新行。我正在使用它在我的单元格中创建中断,但它不起作用
$groupedCell = implode('\r',$data);
I'm pretty sure the code is correct as its placing \r where I would like a carriage return but not the actual return i'm looking for. I've tried some different encodings but still no luck, I am testing in Open Office which I guess could be the issue but I would assume it can handle carriage returns within a cell and I haven't seen any documentation to support otherwise. Thanks for reading!
我很确定代码是正确的,因为它将 \r 放置在我想要回车而不是我正在寻找的实际返回的位置。我尝试了一些不同的编码,但仍然没有运气,我正在 Open Office 中进行测试,我猜这可能是问题所在,但我认为它可以处理单元格内的回车,而且我还没有看到任何支持其他方式的文档。谢谢阅读!
回答by Rudu
The CSV specis one I find implemented in many different ways... it basically seems like it's only half-speced which is frustrating given it's popularity.
该CSV规范是一个我发现它在许多不同的方式实现......这基本上好像它只有一半speced这是令人沮丧的给它的受欢迎程度。
To include a new-line within a cell in a CSV there cell may need to be wrapped, or the new-line may need to be escaped. You'll notice from the linked doc there are three ways to do this - and different programmes treat it differently:
要在 CSV 的单元格中包含换行符,可能需要换行单元格,或者可能需要对换行符进行转义。您会从链接的文档中注意到有三种方法可以做到这一点 - 不同的程序对其有不同的处理方式:
- Excel wraps the whole cell in double quotes: a cell can have (unescaped) newline characters within it and be considered a single cell, as long as it's wrapped in double quotes (note also you'll need to use excel-style double quote escaping within the cell contents)
- Other programmes insert a single backslash before the character, therefore a line ending in
\
is not considered the end of a line, but a newline character within the cell. A cell can have unescaped newline characters within as long as they're preceded by the backslash character. - Others still replace a newline with C-style character escaping, the actual character sequence
\n
or\r\n
. In this case the cell has fully escaped newline characters.
- Excel 将整个单元格用双引号括起来:一个单元格可以在其中包含(未转义的)换行符并被视为单个单元格,只要它用双引号括起来(另请注意,您还需要使用 excel 样式的双引号转义在单元格内容中)
- 其他程序在字符前插入一个反斜杠,因此以 结尾的行
\
不被视为行尾,而是单元格内的换行符。单元格内可以包含未转义的换行符,只要它们前面有反斜杠字符。 - 其他人仍然用 C 风格的字符转义、实际字符序列
\n
或\r\n
. 在这种情况下,单元格已完全转义换行符。
The problem is compounded by the potential need to escape the control characters (as well as other content (eg "
in #1, and \
in #2+3) and different styles of escaping (eg. an embedded quote could be escaped as: double double quote ""
or backslash-double quote \"
)
由于可能需要转义控制字符(以及其他内容(例如"
在 #1 和\
#2+3 中)和不同风格的转义(例如,嵌入的引号可以转义为:double double报价""
或反斜杠双引号\"
)
My advice: generate an open-office document with multiple lines and key escape characters and see how open-office generates a CSV
file. From there you can decide which of the above methods to use for newlines within cells, and which escaping method.
我的建议:生成一个多行和键转义字符的开放式办公室文档,看看开放式办公室如何生成一个CSV
文件。从那里您可以决定将上述哪种方法用于单元格内的换行符,以及哪种转义方法。
example of style-1 (excel):
样式 1 (excel) 示例:
#num,str,num
1,"Hello
World",1990
2,"Yes",1991
example of style-2:
样式 2 的示例:
#num,str,num
1,Hello \
Word,1990
2,Yes,1991
example of style-3:
样式 3 的示例:
#num,str,num
1,Hello \nWorld,1990
2,Yes,1991
回答by meagar
You need to use "\r"
. You can't use escaped characters (aside from \'
) in single quoted strings. '\n'
and '\r'
are a literal backslash followed by an n
or r
, while "\n"
and "\r"
are newlines and carriage returns respectively.
您需要使用"\r"
. 您不能\'
在单引号字符串中使用转义字符(除了)。 '\n'
and'\r'
是文字反斜杠后跟一个n
or r
,而"\n"
and"\r"
分别是换行符和回车符。
As for inserting new lines in your CSV file, it's up to your implementation. There is no standard for CSV, so you'll have to figure out what format to use based on the system you're supplying the CSV data to. Some might accept a '\n'
sequence and interpret it as a new line, others might allow a literal newline provided the cell is enclosed in quotes, and still others will not accept new lines at all.
至于在 CSV 文件中插入新行,则取决于您的实施。CSV 没有标准,因此您必须根据向其提供 CSV 数据的系统确定要使用的格式。有些人可能接受一个'\n'
序列并将其解释为新行,其他人可能允许文字换行,前提是单元格用引号括起来,还有一些人根本不接受新行。
回答by Original Paulie D
- Created an Excel 2010 worksheet with 3 columns.
- Added a heading row with literal values: one, two, three
- Added 1 data row with literal values: abc, abc, abc except that within the 2nd column I pressed ALT+ENTER after each letter to create a carriage return and line feed.
- Did SAVE AS > OTHER and choose CSV while ignoring the warnings.
- Examined the CSV data using NOTEPAD++ and clicked the Show All Characters button in toolbar.
- 创建了一个包含 3 列的 Excel 2010 工作表。
- 添加了带有文字值的标题行:一、二、三
- 添加了 1 个带有文字值的数据行:abc、abc、abc,除了在第二列中我在每个字母后按 ALT+ENTER 创建回车和换行。
- 是否另存为 > 其他并在忽略警告的同时选择 CSV。
- 使用 NOTEPAD++ 检查 CSV 数据并单击工具栏中的“显示所有字符”按钮。
One can see the following:
人们可以看到以下内容:
one, two, three[CR][LF]
abc,"a[LF]
b[LF]
c",abc[CR][LF]
Hope this lends more clarify.
希望这能提供更多的澄清。