word转html方法

在网上看了一篇关于word转html的文章,感觉不错,和大家分享一下。

 1      /// <summary> 
 2         /// word转成html 
 3         /// </summary> 
 4         /// <param name="wordfilename">word文件路径</param> 
 5         private string ConvertWordToHtml(object wordfilename)
 6         {
 7             //在此处放置用户代码以初始化页面 
 8             word.ApplicationClass word = new word.ApplicationClass();
 9             Type wordtype = word.GetType();
10             word.Documents docs = word.Documents;
11             //打开文件 
12             Type docstype = docs.GetType();
13             word.Document doc = (word.Document)docstype.InvokeMember("open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new object[] { wordfilename, true, true });
14             //转换格式,另存为 
15             Type doctype = doc.GetType();
16             string wordsavefilename = wordfilename.ToString();
17             string strsavefilename = wordsavefilename.Substring(0, wordsavefilename.Length - 3) + "html";
18             object savefilename = (object)strsavefilename;
19             doctype.InvokeMember("saveas", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { savefilename, WdSaveFormat.wdFormatFilteredHTML });
20             doctype.InvokeMember("close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
21             //退出 word 
22             wordtype.InvokeMember("quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
23             return savefilename.ToString();
24         }
word转html方法

在此之前需要做一些准备工作

1、必须有office组件。

2、添加引用如下:

 1   using System;
 2   using System.Collections;
 3   using System.Configuration;
 4   using System.Data;
 5   using System.Web;
 6   using System.Web.Security;
 7   using System.Web.UI;
 8   using System.Web.UI.HtmlControls;
 9   using System.Web.UI.WebControls;
10   using System.Web.UI.WebControls.WebParts;
11   using word = Microsoft.Office.Interop.Word;
12   using Microsoft.Office.Interop.Word;
页面引用命名空间

3、项目添加Microsoft.Office.Interop.Word引用。

4、如果代码  word.ApplicationClass word = new word.ApplicationClass();提示无法嵌入互操作类型,则右键点击Microsoft.Office.Interop.Word组件选择属性,将嵌入互操作类型改为False即可解决问题。

完成之后,在word文件所在文件夹中会出现同名的html文件。但是有一个缺点,就是word排版格式不能保存。稍后改进。

转载于:https://www.cnblogs.com/AlphaThink-AT003/p/3336463.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值