zoukankan      html  css  js  c++  java
  • 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排版格式不能保存。稍后改进。

  • 相关阅读:
    JSTL之迭代标签库
    java中的IO流
    浅谈代理模式
    TreeSet集合
    网络编程之UDP协议
    Java中的多线程
    Java中的面向对象
    JavaScript 函数表达式
    JavaScript 数据属性和访问器属性
    JavaScript 正则表达式语法
  • 原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3336463.html
Copyright © 2011-2022 走看看