在java中将字符串写入rtf不会保留结构
我编写了一个 FileMerge 类,在其中获取 rtf 并用 SQL 数据替换某些标签。 一切正常,但有一部分我用一行或多行替换一个标签,它替换了文件,但在 Word 中打开时 \n 或 \r\n 不会创建新行。
代码:
String otherFamilyString="\n";
for(int i=0;i<OtherFamily.size();i=i+TotalArguments)
{
otherFamilyString += "\n"+OtherFamily.get(i+FamilyMbrId) +"|\t"+ OtherFamily.get(i+FamilyMbrName) +"|\t"+ OtherFamily.get(i+FamilyStatus)+"|\t"
+ OtherFamily.get(i+FamilyCoveragetype) +"| "+ OtherFamily.get(i+FamilyRelationship) +"|" ;
System.out.print(i+" "+otherFamilyString);
}
newtext = newtext.replaceAll("OTHERFAMILY",otherFamilyString);
FileWriter writer = new FileWriter(outPutDir+memberInfo.get(GroupId).replace(" ", "")+newName.replace("*", ""));
writer.write(newtext);//write the new file
writer.close();
我也尝试了 System.getProperty("line.separator") 未成功。
当我在记事本中打开文件时,我可以看到所有代码都是富文本格式,除了这部分以正确的结构显示之外。 我想我需要一种方法以富文本格式编写此字符串,以便它在 Word 中显示良好。
谢谢你的帮助, 伊丹.
I wrote a FileMerge class where I take an rtf and replace certain tags with SQL data.
Everything is ok but one part in which I replace one tag with one or more lines, it replaces the file but \n or \r\n doesn't create a new line when opened in word.
the code:
String otherFamilyString="\n";
for(int i=0;i<OtherFamily.size();i=i+TotalArguments)
{
otherFamilyString += "\n"+OtherFamily.get(i+FamilyMbrId) +"|\t"+ OtherFamily.get(i+FamilyMbrName) +"|\t"+ OtherFamily.get(i+FamilyStatus)+"|\t"
+ OtherFamily.get(i+FamilyCoveragetype) +"| "+ OtherFamily.get(i+FamilyRelationship) +"|" ;
System.out.print(i+" "+otherFamilyString);
}
newtext = newtext.replaceAll("OTHERFAMILY",otherFamilyString);
FileWriter writer = new FileWriter(outPutDir+memberInfo.get(GroupId).replace(" ", "")+newName.replace("*", ""));
writer.write(newtext);//write the new file
writer.close();
I also tried System.getProperty("line.separator") unsuccessfully.
When I open the file in notepad, I can see that all of the code is in rich text except this part which shows in the right structure.
I think I need a way to write this string in rich text format so it will show good in word.
Thank you for the help,
Idan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下是一些与 RTF 格式相关的文档: http: //search.cpan.org/~sburke/RTF-Writer/lib/RTF/Cookbook.pod#RTF_Document_Structure
它说使用指定换行符
\line
命令。Here is some documentation pertaining to the RTF format: http://search.cpan.org/~sburke/RTF-Writer/lib/RTF/Cookbook.pod#RTF_Document_Structure
It says to specify a newline using a
\line
command.在 RTF 中,换行符不是由文本中的新行指示的 - 相反,您必须使用正确的控制序列来执行此操作。从浏览 规范 来看,
\line
看起来像硬换行符和类似于段落符的\par
。In RTF, line breaks are not indicated by a new line in the text - instead, you must use the right control sequence to do this. From skimming over the specification,
\line
looks like a hard line break, and\par
like a paragraph break.