专业网站建设品牌,十四年专业建站经验,服务6000+客户--广州京杭网络
免费热线:400-683-0016      微信咨询  |  联系我们

请问如何将word转化为HTML_CSS学习

当前位置:网站建设 > 技术支持
资料来源:网络整理       时间:2023/3/4 6:43:43       共计:3586 浏览

请问如何将word转化为HTML?

页面代码:

[html] view plaincopy

Choose File

C#代码:

[csharp] view plaincopy

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)

{

}

///

/// 将word转换为Html

///

///

///

protected void btnConvert_Click(object sender, EventArgs e)

{

try

{

//上传

//uploadWord(File1);

//转换

wordToHtml(File1);

}

catch (Exception ex)

{

throw ex;

}

finally

{

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

}

}

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

///

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

///

///word文档在客户机的位置

///上传的html文件的地址

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";

}

}

版权说明:
本网站凡注明“广州京杭 原创”的皆为本站原创文章,如需转载请注明出处!
本网转载皆注明出处,遵循行业规范,如发现作品内容版权或其它问题的,请与我们联系处理!
欢迎扫描右侧微信二维码与我们联系。
·上一条:cad怎么把点变大_CSS学习 | ·下一条:spinner前叉怎么样_vue

Copyright © 广州京杭网络科技有限公司 2005-2024 版权所有    粤ICP备16019765号 

广州京杭网络科技有限公司 版权所有