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排版格式不能保存。稍后改进。

  • 相关阅读:
    后缀数组 (Suffix Array) 学习笔记
    Miller-Rabin 素性测试 与 Pollard Rho 大整数分解
    [ USACO 2013 OPEN ] Photo
    清华集训2016做题记录
    「UNR#2」黎明前的巧克力
    「UNR#1」奇怪的线段树
    Atcoder Grand Contest 018 E
    「NOI2015」小园丁与老司机
    「集训队作业2018」三角形
    Codeforces 878 E. Numbers on the blackboard
  • 原文地址:https://www.cnblogs.com/AlphaThink-AT003/p/3336463.html
Copyright © 2011-2022 走看看