如何把richtextbox的内容转成html格式的?
如何把richtextbox的内容转成html格式的? 问题点数:100、回复次数:9Top
1 楼redcaff_l(热的咖啡)回复于 2002-07-11 11:17:09 得分 0
RichTextBox里面的内容是包含了html标签的,直接保存就可以了。Top
2 楼frb_csharp(西沙坡)回复于 2002-07-11 11:22:14 得分 0
我的方法笨,但如果有帮助,请给分鼓励一下。
首选就是操作一个文本文件的问题,在文前和文后加上HTML的标记(必要的即可,如果有更多格式要求方法类似),如<html></html><body></body><title></title>....
然后就是一个操作文件的问题,把这个文件扩展名改成html。
实际上,格示已经是HTML的了,用IE可打开,毕竟HTML也是文本,说到底。Top
3 楼sinsky(十方)回复于 2002-07-11 12:06:17 得分 0
楼上的,这也太简单了吧,这也要用到richtextbox吗?用textbox不就行了?
关键是要把richtextbox里的格式也转换过来,如粗体、颜色等等...
关注,顶一下Top
4 楼andymei(暗黑魔法师)回复于 2002-07-12 08:38:44 得分 0
就是这个意思,帮帮忙吧。
再顶一下Top
5 楼sunjiujiu(绿茶狂人@抵制日货)回复于 2002-07-12 09:33:14 得分 0
frb_csharp(西沙坡) 的思路应该是对的,至于字体大小、字体的颜色你可以实用相关的方法得到,然后写入到html文件的相关的标签中去。思路就是这样,具体实现自己作吧。(和记事本一样,richtextbox的字体颜色、字体大小等等通篇是一样的)
textbox和richtextbox的区别一个是文本的最大长度是不一样的,后者要大的多。其次,后者的文本内容的读取通过Lines数组一行行的读出来。Top
6 楼andymei(暗黑魔法师)回复于 2002-07-25 08:52:56 得分 0
就用个简单的例子来说吧
在richtextbox里,内容是"123456",但"123"是红色的字,
我怎么通过程序把它转成
<font color=red>123</font>456
的格式呢?Top
7 楼harveyzh(zgh)回复于 2002-07-25 13:25:43 得分 50
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, 别忘了共享! -;)Top
8 楼zeaing()回复于 2002-07-25 15:11:08 得分 50
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");
Top




