Java 如何在将内容粘贴到 Excel 文件时从字符串中删除回车符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1612808/
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
How to remove the carriage return from string while pasting content to Excel file
提问by chaithu
I have a problem while pasting my contents (or text) generated by Java code into excel.
The problem is that my Java code generates a String with multiple lines, i.e. with line breaks (\n
) included. When I try to copy this content and paste it into an Excel file, I am getting a multiline text with a square box symbol. I came to know that Windows uses \r\n
for line breaks and not just \n
. I tried to replace my \n
with \r\n
and paste the generated text, but I am getting the same square boxes in my Excel file. Here is my sample code:
将 Java 代码生成的内容(或文本)粘贴到 excel 时遇到问题。问题是我的 Java 代码生成了一个多行字符串,即包含换行符 ( \n
)。当我尝试复制此内容并将其粘贴到 Excel 文件中时,我得到了一个带有方框符号的多行文本。我开始知道 Windows 用于\r\n
换行,而不仅仅是\n
. 我试图替换我\n
的\r\n
并粘贴生成的文本,但我的 Excel 文件中出现了相同的方框。这是我的示例代码:
String myString = "a1\nb1";
String tmpString =myString.replace("\n","\r\n");
System.out.println( "Original = " +"\""+myString+"\"");
System.out.println( "Result = " +"\""+tmpString+"\"");
I have used the " "to wrap the text. When I tried to paste tmpstringin Excel, I got the square box. How can I remove the boxes with multiple lines in my cell?
我已经使用“”来包装文本。当我尝试在 Excel 中粘贴tmpstring时,我得到了方框。如何删除单元格中多行的框?
采纳答案by kdgregory
Do you want the carriage return / newline, or don't you? Your title says that you don't, your code is explicitly adding carriage returns when the string has a newline. If you want to get rid of both, use String.replaceAll(), which takes a regex:
你想要回车/换行符,还是不想要?您的标题说您没有,您的代码在字符串有换行符时显式添加回车符。如果您想摆脱两者,请使用 String.replaceAll(),它需要一个正则表达式:
public static void main(String[] argv)
throws Exception
{
String s1 = "this\r\nis a test";
String s2 = s1.replaceAll("[\n\r]", "");
System.out.println(s2);
}
This example finds any occurrence of the characters, and deletes them. You probably want to look for the sequence of characters and replace with a space, but I'll leave that up to you: look at the doc for java.util.regex.Pattern
.
此示例查找任何出现的字符,并删除它们。您可能想要查找字符序列并用空格替换,但我会留给您:查看java.util.regex.Pattern
.
And I suspect that the "box" is some other character, not a return or newline.
而且我怀疑“框”是其他字符,而不是返回或换行符。
回答by o.k.w
Why don't you copy and paste as usual, then do a search and replace at Excel?
为什么不像往常一样复制和粘贴,然后在 Excel 中进行搜索和替换?
回答by tzup
If it is any help, I ran this line of code (C#)
如果有帮助,我运行了这行代码(C#)
Clipboard.SetText("1\n2\r\n3");
and then Ctrl-V in Excel and 3 cells got filled with A1 got 1, A2 got 2 and A3 got 3. This means that \n and \r\n are handled as expected by Excel. Java strings containing \n and \r\n should work as well. This leads me to believe something's up with the Excel cell settings. Check if the cells are formatted as text.
然后在 Excel 中按 Ctrl-V 和 3 个单元格填充 A1 得到 1,A2 得到 2 和 A3 得到 3。这意味着 \n 和 \r\n 按预期由 Excel 处理。包含 \n 和 \r\n 的 Java 字符串也应该可以工作。这让我相信 Excel 单元格设置有问题。检查单元格是否格式化为文本。
Other than that, no idea, sorry.
除此之外,不知道,抱歉。
回答by simon
class Test {
public static void main(String args[])
System.out.print("Hello\rHi");
}
Due to \r cursor moves to starting of the current line, so "He" of Hello is replaced by "Hi".
由于\r 光标移动到当前行的开头,所以Hello 的“He”被“Hi”代替。
回答by Mr. Effting
Try this =)
试试这个 =)
String s1 = "this\r\nis a test";
String s2 = s1.replaceAll("(\r|\n)", "");
System.out.println(s2);