rtf文本格式如何转换html格式(c#代码实现)

rtf文本格式如何转换html格式(c#代码实现),第1张

将RTF格式文件转成HTML并在网页中显示的代码 它是这样工作的,将一个RTF文件上传,然后转成HTML显示出来,代码有点复杂,teaman还要好好研究,好象中文版有点问题。 <html><body><? if(!($userfile)) { ?><form enctype="multipart/form-data" action="<?print($PHP_SELF)?>" method=post><input type=hidden name="max_file_size" value=2000>Send this file: <input name=userfile type=file><input type=submit value=Upload></form></body></html><? exit} function ProcessTags($tags, $line) { $html = ""global $colorglobal $sizeglobal $bullets// Remove spaces. $tags = trim($tags)// Found the beginning of the bulleted l // ist. if(ereg("\\\pnindent", $tags)) { $html .= "<ul><li>"$bullets += $line$tags = ereg_replace("\\\par", "", $tags)$tags = ereg_replace("\\\(tab)", "", $tags)} if($line - $bullets == 0) { $tags = ereg_replace("\\\par", "", $tags)} elseif($line - $bullets == 1) { if(ereg("\\\pntext", $tags)) { $html .= "<li>"$tags = ereg_replace("\\\par", "", $tags)$tags = ereg_replace("\\\(tab)", "", $tags)$bullets++} else { $html .= "</ul>"$bullets = 0} } // Convert Bold. if(ereg("\\\b0", $tags)){ $html .= "</b>"} elseif(ereg("\\\b", $tags)) { $html .= "<b>"} // Convert Italic. if(ereg("\\\i0", $tags)){ $html .= "</i>"} elseif(ereg("\\\i", $tags)) { $html .= "<i>"} // Convert Underline. if(ereg("\\\ulnone", $tags)){ $html .= "</u>"} elseif(ereg("\\\ul", $tags)){ $html .= "<u>"} // Convert Alignments. if(ereg("\\\pard\\\qc", $tags)) { $html .= "<div align=center>"} elseif(ereg("\\\pard\\\qr", $tags)) { $html .= "<div align=right>"} elseif(ereg("\\\pard", $tags)){ $html .= "<div align=left>"} // Remove \pard from the tags so it does // n't get confused with \par. $tags = ereg_replace("\\\pard", "", $tags)// Convert line breaks. if(ereg("\\\par", $tags)){ $html .= "<br>"} // Use the color table to capture the fo // nt color changes. if(ereg("\\\cf[0-9]", $tags)) { global $fcolor$numcolors = count($fcolor)for($i = 0$i <$numcolors$i++) { $test = "\\\cf" . ($i + 1)if(ereg($test, $tags)) { $color = $fcolor[$i]} } } // Capture font size changes. if(ereg("\\\fs[0-9][0-9]", $tags, $temp)) { $size = ereg_replace("\\\fs", "", $temp[0])$size /= 2if($size <= 10) { $size = 1} elseif($size <= 12) { $size = 2} elseif($size <= 14) { $size = 3} elseif($size <= 16) { $size = 4} elseif($size <= 18) { $size = 5} elseif($size <= 20) { $size = 6} elseif($size <= 22) { $size = 7} else{ $size = 8} } // If there was a font color or size cha // nge, change the font tag now. if(ereg("(\\\cf[0-9])||(\\\fs[0-9][0-9])", $tags)) { $html .= "</font><font size=$size color=$color>"} // Replace \tab with alternating spaces // and nonbreakingwhitespaces. if(ereg("\\\(tab)", $tags)) { $html .= ""} return $html} function ProcessWord($word) { // Replace \\ with \ $word = ereg_replace("[\\]{2,}", "\\", $word)// Replace \{ with { $word = ereg_replace("[\\][\{]", "\{", $word)// Replace \} with } $word = ereg_replace("[\\][\}]", "\}", $word)// Replace 2 spaces with one space. $word = ereg_replace(" ", " ", $word)return $word} $color = "000000"$size = 1$bullets = 0// Read the uploaded file into an array. // $rtfile = file($userfile)$fileLength = count($rtfile)// Loop through the rest of the array for($i = 1$i <$fileLength$i++) { /* ** If the line contains "\colortbl" then we found the color table. ** We'll have to split it up into each individual red, green, and blue ** Convert it to hex and then put the red, green, and blue back together. ** Then store each into an array called fcolor. */ if(ereg("^\{\\\colortbl", $rtfile[$i])) { // Split the line by the backslash. $colors = explode("\\", $rtfile[$i])$numOfColors = count($colors)for($k = 2$k <$numOfColors$k++) { // Find out how many different colors th // ere are. if(ereg("[0-9]+", $colors[$k], $matches)) { $match[] = $matches[0]} } // For each color, convert it to hex. $numOfColors = count($match)for($k = 0$k <$numOfColors$k += 3) { $red = dechex($match[$k])$red = $match[$k] <16 ? "0$red" : $red$green = dechex($match[$k + 1])$green = $match[$k +1] <16 ? "0$green" : $green$blue = dechex($match[$k + 2])$blue = $match[$k + 2] <16 ? "0$blue" : $blue$fcolor[] = "$red$green$blue"} $numOfColors = count($fcolor)} // Or else, we parse the line, pulling o // ff words and tags. else { $token = ""$start = 0$lineLength = strlen($rtfile[$i])for($k = 0$k <$lineLength$k++) { if($rtfile[$i][$start] == "\\" &&$rtfile[$i][$start + 1] != "\\") { // We are now dealing with a tag. $token .= $rtfile[$i][$k]if($rtfile[$i][$k] == " ") { $newFile[$i] .= ProcessTags($token, $i)$token = ""$start = $k + 1} elseif($rtfile[$i][$k] == "\n") { $newFile[$i] .= ProcessTags($token, $i)$token = ""} } elseif($rtfile[$i][$start] == "{") { // We are now dealing with a tag. $token .= $rtfile[$i][$k]if($rtfile[$i][$k] == "}") { $newFile[$i] .= ProcessTags($token, $i)$token = ""$start = $k + 1} } else { // We are now dealing with a word. if($rtfile[$i][$k] == "\\" &&$rtfile[$i][$k + 1] != "\\" &&$rtfile[$i][$k - 1] != "\\") { $newFile[$i] .= ProcessWord($token)$token = $rtfile[$i][$k]$start = $k} else { $token .= $rtfile[$i][$k]} } } } } $limit = sizeof($newFile)for($i = 0$i <$limit$i++) { print("$newFile[$i]\n")} ?></body></html>

不行的话试试这个

{

//创建一个word的实例

Word.Application newApp = new Word.Application()

// 指定源文件和目标文件

object Source="c:\\abc\\Source.doc"

object Target="c:\\abc\\Target.rtf"

object Unknown =Type.Missing

// 打开要转换的Word文件

newApp.Documents.Open(ref Source,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown )

// 指定文档的类型

object format = Word.WdSaveFormat.wdFormatRTF

//改变文档类型

newApp.ActiveDocument.SaveAs(ref Target,ref format,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown,

ref Unknown,ref Unknown,ref Unknown)

//关闭word实例

newApp.Quit(ref Unknown,ref Unknown,ref Unknown)

}

}

}

或是这种方法

在richtohtml函数中增加下面的代码就行了:

Dim strLastFontsize As Long

If strLastFontsize <>rtbRichTextBox.SelFontSize Then

strLastFontsize = rtbRichTextBox.SelFontSize

strHTML$ = strHTML$ + "<font-size=""" &strLastFontsize &""">"

End If

将RTF格式转换成普通的word格式方法如下:

启动word软件,打开rtf格式的文档,按下键盘F12,d出另存为对话框,选择格式.doc或.docx格式,就实现了将rtf格式转换成普通的word格式。

日常生活中,我们总是在Word中进行文字的编辑,它不仅能够保存Text文本,还可以保存文本的格式等等。那么如果我要将一Word文档上的内容展示在网页上,该怎么做呢?这里我提供了一个小工具,你可以将Word转换为Html,需要显示的话,可以直接访问该Html,废话不多说,下面看代码。

页面代码:

[html] view plaincopy

<span style="font-size:18px"><div>

<input id="File1" type="file" runat="server"/>

<asp:Button ID="btnConvert" runat="server" Text="转换" OnClick="btnConvert_Click" />

</div></span>

C#代码:

[csharp] view plaincopy

<span style="font-size:18px">using System

using System.Data

using System.Configuration

using System.Collections

using System.Collections.Generic

using System.Linq

using System.Web

using System.Web.Security

using System.Web.UI

using System.Web.UI.WebControls

using System.Web.UI.WebControls.WebParts

using System.Web.UI.HtmlControls

using System.IO

protected void Page_Load(object sender, EventArgs e)

{

}

/// <summary>

/// 将word转换为Html

/// </summary>

/// <param name="sender"></param>

/// <param name="e"></param>

protected void btnConvert_Click(object sender, EventArgs e)

{

try

{

//上传

//uploadWord(File1)

//转换

wordToHtml(File1)

}

catch (Exception ex)

{

throw ex

}

finally

{

Response.Write("恭喜,转换成功!")

}

}

//上传文件并转换为html wordToHtml(wordFilePath)

///<summary>

///上传文件并转存为html

///</summary>

///<param name="wordFilePath">word文档在客户机的位置</param>

///<returns>上传的html文件的地址</returns>

public string wordToHtml(System.Web.UI.HtmlControls.HtmlInputFile wordFilePath)

{

Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass()

Type wordType = word.GetType()

Microsoft.Office.Interop.Word.Documents docs = word.Documents

// 打开文件

Type docsType = docs.GetType()

//应当先把文件上传至服务器然后再解析文件为html

string filePath = uploadWord(wordFilePath)

//判断是否上传文件成功

if (filePath == "0")

return "0"

//判断是否为word文件

if (filePath == "1")

return "1"

object fileName = filePath

Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",

System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true })

// 转换格式,另存为html

Type docType = doc.GetType()

string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() +

System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString()

// 判断指定目录下是否存在文件夹,如果不存在,则创建

if (!Directory.Exists(Server.MapPath("~\\html")))

{

// 创建up文件夹

Directory.CreateDirectory(Server.MapPath("~\\html"))

}

//被转换的html文档保存的位置

string ConfigPath = HttpContext.Current.Server.MapPath("html/" + filename + ".html")

object saveFileName = ConfigPath

/*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成:

* docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,

* null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML})

* 其它格式:

* wdFormatHTML

* wdFormatDocument

* wdFormatDOSText

* wdFormatDOSTextLineBreaks

* wdFormatEncodedText

* wdFormatRTF

* wdFormatTemplate

* wdFormatText

* wdFormatTextLineBreaks

* wdFormatUnicodeText

*/

docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,

null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML })

//关闭文档

docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,

null, doc, new object[] { null, null, null })

// 退出 Word

wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null)

//转到新生成的页面

return ("/" + filename + ".html")

}

public string uploadWord(System.Web.UI.HtmlControls.HtmlInputFile uploadFiles)

{

if (uploadFiles.PostedFile != null)

{

string fileName = uploadFiles.PostedFile.FileName

int extendNameIndex = fileName.LastIndexOf(".")

string extendName = fileName.Substring(extendNameIndex)

string newName = ""

try

{

//验证是否为word格式

if (extendName == ".doc" || extendName == ".docx")

{

DateTime now = DateTime.Now

newName = now.DayOfYear.ToString() + uploadFiles.PostedFile.ContentLength.ToString()

// 判断指定目录下是否存在文件夹,如果不存在,则创建

if (!Directory.Exists(Server.MapPath("~\\wordTmp")))

{

// 创建up文件夹

Directory.CreateDirectory(Server.MapPath("~\\wordTmp"))

}

//上传路径 指当前上传页面的同一级的目录下面的wordTmp路径

uploadFiles.PostedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName))

}

else

{

return "1"

}

}

catch

{

return "0"

}

//return "http://" + HttpContext.Current.Request.Url.Host + HttpContext.Current.Request.ApplicationPath + "/wordTmp/" + newName + extendName

return System.Web.HttpContext.Current.Server.MapPath("wordTmp/" + newName + extendName)

}

else

{

return "0"

}

}</span>


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/6302671.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-20
下一篇 2023-03-20

发表评论

登录后才能评论

评论列表(0条)

保存