首先找到一个引用,这个引用2015上的vs都有的,诺,就是这个!

using Microsoft.Office.Interop.Word;

首选准备好你的word这里做测试呢 就在项目里面创建一个文件夹,给你要转换的word放到里面,

其次copy下面这段方法到你的项目里面

 1         private string GetPathByDocToHTML(string strFile)
 2         {
 3             if (string.IsNullOrEmpty(strFile))
 4             {
 5                 return "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;
11
12             // 打开文件
13             Type docsType = docs.GetType();
14
15             object fileName = strFile;
16
17             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 });
19
20             // 转换格式,另存为html
21             Type docType = doc.GetType();
22             //给文件重新起名
23             string 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();
25
26             string strFileFolder = "/html/";
27             DateTime dt = DateTime.Now;
28             //以yyyymmdd形式生成子文件夹名
29             string 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();
32             string strFilePath = strFileFolder + strFileSubFolder + "/";
33             // 判断指定目录下是否存在文件夹,如果不存在,则创建
34             if (!Directory.Exists(Server.MapPath(strFilePath)))
35             {
36                 // 创建up文件夹
37                 Directory.CreateDirectory(Server.MapPath(strFilePath));
38             }
39
40             //被转换的html文档保存的位置
41             // HttpContext.Current.Server.MapPath("html" + strFileSubFolder + filename + ".html")
42             string ConfigPath = Server.MapPath(strFilePath + filename + ".html");
43             object saveFileName = ConfigPath;
44
45             /*下面是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,
61             null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
62
63             //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
64             //  null, doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
65
66             //关闭文档
67             docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
68             null, doc, new object[] { null, null, null });
69
70             // 退出 Word
71             wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
72             //转到新生成的页面
73             //return ("/" + filename + ".html");
74
75             //转化HTML页面统一编码格式
76             TransHTMLEncoding(ConfigPath);
77
78             return (strFilePath + filename + ".html");
79         }
80         private void TransHTMLEncoding(string strFilePath)
81         {
82             try
83             {
84                 System.IO.StreamReader sr = new System.IO.StreamReader(strFilePath, Encoding.GetEncoding(0));
85                 string 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);
89
90                 sw.Write(html);
91                 sw.Close();
92             }
93             catch (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页面相关推荐

  1. 如何将word文档放入html页面,怎么将Word文档转换为Web页面

    为了方便将Word文档在互联网上和局域网上发布,需要将文档保存为Web页面文件.将文档保存为Web页面,这种页面文件使用HTML文件格式.以下是学习啦小编为您带来的关于将Word文档转换为Web页面的 ...

  2. Word处理控件Aspose.Words功能演示:在 Python 中将 Word 文档转换为 PNG、JPEG 或 BMP

    MS Word 文件到图像格式的转换让您可以将文档的页面嵌入到您的 Web 或桌面应用程序中.为了在 Python 应用程序中执行此转换,本文介绍了如何使用 Python 将 Word DOCX或DO ...

  3. 三种方法将Word文档转换为PDF文件格式

    如何将Word文档转换为PDF文件格式呢?大家在传输文件时,很多人喜欢使用PDF文件格式,因为它非常稳定,不会出现格式混乱的问题.但有些人可能不知道如何进行转换,今天我将介绍三种转换方法,让我们一起来 ...

  4. 将Word文档转换为PDF文件的步骤和技巧

    在日常工作和学习中,我们经常需要将Word文档转换为PDF文件.PDF文件的格式稳定.排版精美.易于共享等特点使其成为了许多场合下的首选文件格式.在本文中,我将为大家介绍将Word文档转换为PDF文件 ...

  5. Java 将 Word 文档转换为 PDF 的完美工具

    点击上方 好好学java ,选择 星标 公众号 重磅资讯.干货,第一时间送达 今日推荐:为什么魂斗罗只有 128 KB却可以实现那么长的剧情?个人原创+1博客:点击前往,查看更多 来源:https:/ ...

  6. 如何将Microsoft Word文档转换为PDF

    PDFs are handy for distributing documents so that they're seen the same way by all parties. Typicall ...

  7. azw3转换为pdf_干货:如何Java 将 Word 文档转换为 PDF

    在日常工作中,PDF格式良好的视觉阅读性和稳定性使其使用越来越广泛.因此我们常会遇到需要将成型的Word文档转换为PDF格式的情况.本文就将通过使用Java程序来演示如何将Word文档转换成PDF格式 ...

  8. Java将Word文档转换为PDF的完美工具

    引用至:https://mp.weixin.qq.com/s/JIgo3f98HufGJx23mgtvag Java 将 Word 文档转换为 PDF 的完美工具 在日常工作中,PDF格式良好的视觉阅 ...

  9. html word页面展示,word文档怎样调页面

    word文档如何调整页面?在页面设置对话框的"页边距2003版"选项卡中选择"方向"为横向或是纵向. Word中怎么设置一个页面显示4页 Word一页只显示四页 ...

最新文章

  1. 主成分分析(Principal components analysis)-最大方差解释
  2. 异常:java.lang.IllegalArgumentException: Control character in cookie value or attribute.
  3. gj4 深入类和对象
  4. 程序员面试金典 - 面试题 17.06. 2出现的次数(找递推规律)
  5. 2022年薪百万赛道:高性能神经网络与AI芯片应用
  6. java日期函数精确到日,java日期时间函数分享
  7. 95-190-032-源码-window-CountWindow
  8. (寻找第K小的数amp;amp;寻找第K小的数的和)
  9. Hadoop配置文件( hadoop-env.sh、core-site.xml、hdfs-site.xm、mapred-site.xml、yarn-site.xml、 slaves)详解
  10. 轮播图背景图铺满整个div
  11. 苹果电脑如何正确断网 Mac电脑断网方法
  12. 关于连接数据库出现时差问题的解决方法
  13. linux遇到hint:num lock on
  14. 职场故事让你如梦初醒
  15. 运维的升级打怪之路v2版
  16. 李沐动手学深度学习v2-目标检测中的锚框和代码实现
  17. 前端获取计算机设备信息,前端获取设备/APP信息
  18. 淘宝代购系统、海外代购系统、代购小程序、APP的开发以及源码PHP前端源码
  19. uniapp生成 NFC 卡片 例子
  20. MATLAB学习——数据类型(结构体、数组、单元数组、map容器类型)

热门文章

  1. 转载自科技猿人:联想5G投票这事,我们就来彻底讲清楚
  2. [Discuz!模板] 听雨轩风格[GBK][utf8][一键安装 DIY][20110918更新]
  3. python爬虫笔记——Scrapy框架(浅学)
  4. python中area是什么意思_python之懒惰属性(延迟初始化)
  5. vue练手小项目--眼镜在线试戴
  6. Windows下维基百科中文语料库词向量训练
  7. kali下载速度慢_kali Linux 2020.1最新安装教程,绝对能帮你安装好!不是root、没有桌面、中文乱码、下载太慢、ssh链接等问题!...
  8. java nio MappedByteBuffer 文件映射
  9. 美IT业25大秘密:Facebook耗时一周建成
  10. 基于芯科Host-NCP解决方案的Zigbee 3.0 Gateway技术研究(-)-Z3GatewayHost应用