将txt文件导入excel使换行符消失
我正在尝试将文本文件导入 Excel (2007)。该文件是从 C# 文本框导出的,并且包含换行符。虽然当我导入它时(使用excel自带的文本导入向导),换行符完全消失了。我不想编写 VBA 文件并放入 excel 文件中运行,而是在将文本框数据转换为 txt 文件之前,使用 C# 中的简洁方法更改它。这有可能吗?
I am trying to import a text file into excel (2007). The file was exported from a C# text box and it contains linebreaks. Although when I import it (with the text import wizard that comes with excel), the linebreaks disappears completely. I would prefer not to have to write a VBA file and place in an excel file to run but instead change this with a neat method in C#, before it turns the text box data into a txt file. Is this possible in any way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我明白了这一点。如果您在文本两边加上引号,则任何嵌入换行符 (ASCII 010) 都将作为嵌入换行符导入到 Excel 中。换句话说,这些换行不会导致文本跨 Excel 行拆分。
尝试一下。在 Notepad.exe 中创建两个文件。在第一个中,按 Alt-0010 终止第一行:
测试行 1 以 alt-0010 终止
测试行 2
在第二个中,以 " 开始行并以 " 终止。对于第一行,在“:
“测试行 1 在引用之前以 alt-0010 终止”之前插入 Alt-0010
“测试行 2”
现在导入两者导入 Excel 并查看差异,
了解更多信息。
I figured this out. If you put quotes around text any embedded line feeds (ASCII 010) will be imported into Excel as embedded line feeds. In other words, these line feeds will not cause the text to split across Excel rows.
Try it. Create two files in Notepad.exe. In the first terminate the first line by pressing Alt-0010:
Test line 1 terminated with alt-0010
Test line 2
In the second, begin lines with " and terminate with ". For the first line insert an Alt-0010 just before the ":
"Test line 1 terminated with alt-0010 prior to the quote"
"Test line 2"
Now import both into Excel and see the difference.
See IETF RFC 4180 for more information
在 Excel 中,单元格内的换行符被编码为 ascii 代码 10(即 \n)(通过方便地使用宏记录器并检查生成的 VBA 来确定)。我认为新行的“消失”可能是您 C# 发出 \n\r 的结果,因此您可以尝试在输出之前在 C# 代码中用“”全局替换“\r”。
In excel a line break within a cell is encoded as ascii code 10 (i.e. \n) (determined through the handy use of the macro recorder and inspection of the generated VBA). I think the 'disappearance' of new lines probably is a result of you're C# emitting \n\r, so you might try doing a global replacement of "\r" with "" in your C# code before outputting it.
感谢您的帮助!我尝试以 \" 开始文本并以 \" 结束,但是当我转到 Excel 时,我将每一行放在一个单独的单元格中,我希望的计划是将所有文本放在一个 Excel 单元格中。
Thanks for the help! I tried starting the text with \" and finish with \" but when I go to excel, I get each line in a separate cell and my hopeful plan was to get all the text in one single excel cell.