I cannot seem to get any escape characters to work for newlines?

匿名 (未验证) 提交于 2019-12-03 02:38:01

问题:

I'm not sure why the escape characters won't create new lines?

MSWord ITSM = new MSWord("C:/users/o519731/Desktop/ITSM.docx");  XWPFParagraph copyITSM = ITSM.Document.createParagraph(); XWPFRun enterText = copyITSM.createRun();  enterText.setText("First line.\n"); enterText.setText("Second line.\n"); enterText.setText("Third line.");  ITSM.Document.write(ITSM.wordStream); ITSM.wordStream.close(); System.out.println("Document Created."); 

Results in:

First Line.Second line.Third line.

Expecting each on a separate line:

First line. Second line. Third line.

回答1:

Use your systems defined newline escape character(s) for newlines. Also escape double quotes if you wish them to appear in the file by adding a \ in front of them.

String newLine = System.getProperty("line.separator"); enterText.setText("\"First line.\""); enterText.addBreak(); enterText.setText("\"Second line.\""); enterText.addBreak(); enterText.setText("\"Third line.\""); enterText.addBreak(); 


回答2:

please escape the quote like

 enterText.setText("\"First line.\n\"");     enterText.setText("\"Second line.\n\"");     enterText.setText("\"Third line.\""); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!