c#怎么将word 转换成html文件

html-css010

c#怎么将word 转换成html文件,第1张

日常生活中,我们总是在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>

下边我把我的实战经验共享一下:

1.

word自动转换布局的功能其实并不是很好,往往造成与word内的布局错乱的情况。让很多本来费了很多心思设计word布局的人非常恼火。对于这种问题,我采用了现在word内设计表格布局的方法,把所有的内容都放在事先画好的表格内,并将表格的边框都设成无。在设计时可以看到边框,在运行时就没有了,非常的便于设计。一切都做好了,就该动手转换成网页了。

2.

word提供了三种转换成网页的方式:

a)

单个文件网页b)

网页c)

筛选过的网页

l

如果选择a,word将会把文件转换为mhtml格式,所有word内的内容都会包含在一个文件里包括图片、表格等;

l

如果选择b,则会把文件转换成一个带word自身标记符的html文件,并产生一个与此相关的网页图片存放文件夹,里边就是word内嵌的图片,但是已经经过了优化。一般不大于60k;

l

如果选择c,则会把文件转换成完全符合html标记的网页,并产生一个与此相关的网页图片存放文件夹,里边就是word内嵌的图片,但是已经经过了优化。一般不大于60k;

3.碰到的难题:要求将word转成单个文件网页――因为带图片文件夹维护起来比较麻烦。但是发现将word转换为单个网页文件后,文件太大,完全不适合在internet上传输。

实战秘技:先将word文件按照c方式转换,会产生一个网页文件和一个图片文件夹。这时将所有的文档都关掉,将产生的新网页文件打开。然后全选(ctrl+a,ctrl+c)网页内容。新建一个word文件,让后将内容拷贝其中(ctrl+v),然后在将这个文件转为

单个网页文件。这时你会惊奇的发现,新产生的单个文件的网页的大小已经变的很小了。