首页 / HTML / c#把word文档转换为html页面
c#把word文档转换为html页面
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了c#把word文档转换为html页面,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4110字,纯文字阅读大概需要6分钟。
内容图文
![c#把word文档转换为html页面](/upload/InfoBanner/zyjiaocheng/1168/c9903dd898494d6ab2558626fd75fbf6.jpg)
首先找到一个引用,这个引用2015上的vs都有的,诺,就是这个!
using Microsoft.Office.Interop.Word;
首选准备好你的word这里做测试呢 就在项目里面创建一个文件夹,给你要转换的word放到里面,
其次copy下面这段方法到你的项目里面
1 private string GetPathByDocToHTML(string strFile) 2 { 3if (string.IsNullOrEmpty(strFile)) 4 { 5return"0";//没有文件 6 } 7 8 Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass(); 9 Type wordType = word.GetType(); 10 Microsoft.Office.Interop.Word.Documents docs = word.Documents; 1112// 打开文件 13 Type docsType = docs.GetType(); 1415object fileName = strFile; 1617 Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", 18 System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true }); 1920// 转换格式,另存为html 21 Type docType = doc.GetType(); 22//给文件重新起名23string filename = System.DateTime.Now.Year.ToString() + System.DateTime.Now.Month.ToString() + System.DateTime.Now.Day.ToString() + 24 System.DateTime.Now.Hour.ToString() + System.DateTime.Now.Minute.ToString() + System.DateTime.Now.Second.ToString(); 2526string strFileFolder = "/html/"; 27 DateTime dt = DateTime.Now; 28//以yyyymmdd形式生成子文件夹名29string strFileSubFolder = dt.Year.ToString(); 30 strFileSubFolder += (dt.Month < 10) ? ("0" + dt.Month.ToString()) : dt.Month.ToString(); 31 strFileSubFolder += (dt.Day < 10) ? ("0" + dt.Day.ToString()) : dt.Day.ToString(); 32string strFilePath = strFileFolder + strFileSubFolder + "/"; 33// 判断指定目录下是否存在文件夹,如果不存在,则创建 34if (!Directory.Exists(Server.MapPath(strFilePath))) 35 { 36// 创建up文件夹 37 Directory.CreateDirectory(Server.MapPath(strFilePath)); 38 } 3940//被转换的html文档保存的位置 41// HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")42string ConfigPath = Server.MapPath(strFilePath + filename + ".html"); 43object saveFileName = ConfigPath; 4445/*下面是Microsoft Word 9 Object Library的写法,如果是10,可能写成: 46 * docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 47 * null, doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML}); 48 * 其它格式: 49 * wdFormatHTML 50 * wdFormatDocument 51 * wdFormatDOSText 52 * wdFormatDOSTextLineBreaks 53 * wdFormatEncodedText 54 * wdFormatRTF 55 * wdFormatTemplate 56 * wdFormatText 57 * wdFormatTextLineBreaks 58 * wdFormatUnicodeText 59*/60 docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 61null, doc, newobject[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); 6263//docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, 64// null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML }); 6566//关闭文档 67 docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, 68null, doc, newobject[] { null, null, null }); 6970// 退出 Word 71 wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null); 72//转到新生成的页面 73//return ("/" + filename + ".html"); 7475//转化HTML页面统一编码格式76 TransHTMLEncoding(ConfigPath); 7778return (strFilePath + filename + ".html"); 79 } 80privatevoid TransHTMLEncoding(string strFilePath) 81 { 82try83 { 84 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0)); 85string html = sr.ReadToEnd(); 86 sr.Close(); 87 html = System.Text.RegularExpressions.Regex.Replace(html, @"<meta[^>]*>", "<meta http-equiv=Content-Type content=‘text/html; charset=gb2312‘>", System.Text.RegularExpressions.RegexOptions.IgnoreCase); 88 System.IO.StreamWriter sw = new System.IO.StreamWriter(strFilePath, false, Encoding.Default); 8990 sw.Write(html); 91 sw.Close(); 92 } 93catch (Exception ex) 94 { 95 Page.ClientScript.RegisterStartupScript(Page.ClientScript.GetType(), "myscript", "<script>alert(‘" + ex.Message + "‘)</script>"); 96 } 97 }
其实是两个方法。
为了测试呢 你需要随便弄个地方调用这个这个方法
string strWord = Server.MapPath("/wordpath/thisisword.doc");
GetPathByDocToHTML(strWord);
就是这个,需要把你的word传进去,当然你可以写在Page_Load里面,测试用运行之后打开你的项目找到你保存的位置就行了。
原文:https://www.cnblogs.com/wangxlei/p/9396196.html
内容总结
以上是互联网集市为您收集整理的c#把word文档转换为html页面全部内容,希望文章能够帮你解决c#把word文档转换为html页面所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。