如何把richtextbox的内容转成html格式的?

andymei 2002-07-11 10:53:19
如何把richtextbox的内容转成html格式的?
...全文
1124 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
andymei 2002-07-25
  • 打赏
  • 举报
回复
就用个简单的例子来说吧
在richtextbox里,内容是"123456",但"123"是红色的字,
我怎么通过程序把它转成
<font color=red>123</font>456
的格式呢?
zeaing 2002-07-25
  • 打赏
  • 举报
回复
harveyzh说的不错。由于RTF和HTML都是开放标准的文档格式,所以外面有很多RTF到HTML的转换器,可以到Google上搜索一下。另外,在developerWorks网站上有一篇怎么利用XSL把RTF转换成HTML的文章:http://www-106.ibm.com/developerworks/xml/library/x-tiprtf/

如果你的机器上面装了Office,那可以利用Word Application Object来打开RTF文件并另存为HTML或者mhtml格式,这样也能完成RTF到HTML的转换:

public void SaveRtfAsHtml(RichTextBox richTextBox,string SaveAsFileName)
{
//保存成一个临时的rtf文件。
string tempFileName=System.IO.Path.GetTempFileName();
this.richTextBox1.SaveFile(tempFileName+".rtf",System.Windows.Forms.RichTextBoxStreamType.RichText);

object Nothing=System.Reflection.Missing.Value;
object srcFileName=tempFileName+".rtf";
object dstFileName=SaveAsFileName;
object format=Word.WdSaveFormat.wdFormatHTML;

//打开刚才保存的rtf文件
Word.Application wordApp=new Word.ApplicationClass();
Word.Document wordDoc=wordApp.Documents.Open(ref srcFileName,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);

//将rtf文件save as成html文件
wordDoc.SaveAs(ref dstFileName,ref format,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing,ref Nothing);
wordDoc.Close(ref Nothing, ref Nothing, ref Nothing);
wordApp.Quit(ref Nothing, ref Nothing, ref Nothing);
}

上面那个SaveRtfAsHtml函数主要做了这么几件事情:
1)把一个richtextbox里面的内容存成一个临时的rtf文件。
2)用word application object打开这个临时的rtf文件。
3)另存为用户指定路径的一个html文件。

例如,可以这样使用这个函数:
SaveRtfAsHtml(this.richTextBox1,@"c:\mydoc.html");
harveyzh 2002-07-25
  • 打赏
  • 举报
回复
1. You can use the RichtextBox.SaveFile() method to save the content in your control in a "RTF" file.

2. According to the RTF specification below, write your own RTF->HTML parser.

http://msdn.microsoft.com/downloads/default.asp?URL=/downloads/sample.asp?url=/MSDN-FILES/027/001/758/msdncompositedoc.xml

同时,你可以在这个规范里面找到RTF解析程序的源代码, 可供你参考。

如果哪位有空实现这个converter, 别忘了共享! -;)
sunjiujiu 2002-07-12
  • 打赏
  • 举报
回复
frb_csharp(西沙坡) 的思路应该是对的,至于字体大小、字体的颜色你可以实用相关的方法得到,然后写入到html文件的相关的标签中去。思路就是这样,具体实现自己作吧。(和记事本一样,richtextbox的字体颜色、字体大小等等通篇是一样的)
textbox和richtextbox的区别一个是文本的最大长度是不一样的,后者要大的多。其次,后者的文本内容的读取通过Lines数组一行行的读出来。
andymei 2002-07-12
  • 打赏
  • 举报
回复
就是这个意思,帮帮忙吧。

再顶一下
sinsky 2002-07-11
  • 打赏
  • 举报
回复
楼上的,这也太简单了吧,这也要用到richtextbox吗?用textbox不就行了?

关键是要把richtextbox里的格式也转换过来,如粗体、颜色等等...

关注,顶一下
frb_csharp 2002-07-11
  • 打赏
  • 举报
回复
我的方法笨,但如果有帮助,请给分鼓励一下。

首选就是操作一个文本文件的问题,在文前和文后加上HTML的标记(必要的即可,如果有更多格式要求方法类似),如<html></html><body></body><title></title>....

然后就是一个操作文件的问题,把这个文件扩展名改成html。

实际上,格示已经是HTML的了,用IE可打开,毕竟HTML也是文本,说到底。
redcaff_l 2002-07-11
  • 打赏
  • 举报
回复
RichTextBox里面的内容是包含了html标签的,直接保存就可以了。

110,567

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧