word转化为html

对于如何将各种电子文本的图书进行预处理,昨天晚上想了一下,想到先将文本转化为html格式,这样在窗体中既好显示,同时也好读取图书的格式和地址

下面是在网上下的转换代码

经过一番调试,总算可以运行成功了

遇到的问题

1.word引用

这个虽说以前是用过的,但是还是印象不深刻,不论添加什么引用最后都提示说找不到命名空间“word”,后来Microsoft.Office.Interop.Word整个来代替word就可以了。

2.Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });这个部分总是调试不通过,后来重新用“生成……”就可以了,虽然不知道是为什么,但总算是可以用了。

下面是源代码

using System;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;

namespace Word2Html
{
    /// <summary>
    /// Word2Html的摘要说明
    /// </summary>
    public partial class Form1 : Form
    {
        private System.Windows.Forms.Button btnWordToHtml;
       
        public Form1()
        {

            InitializeComponent();
        }

 

        private void btnWordToHtml_Click(object sender, System.EventArgs e)
        {
            Microsoft.Office.Interop.Word.ApplicationClass appClass = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = appClass.GetType();
            Microsoft.Office.Interop.Word.Documents docs = appClass.Documents;

            //打开文件
            Type docsType = docs.GetType();
            object fileName = "D://1.doc";
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
                System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fileName, true, true });

            // 转换格式,另存为
            Type docType = doc.GetType();
            object saveFileName = "D://1.html";
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,
                doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });


            //如果是Microsoft Word 9.0 Object Library的写法,可能写成:
            //docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null,
            //doc, new object[]{saveFileName, Word.WdSaveFormat.wdFormatHTML});

            // 退出 Word
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, appClass, null);
        }

    }
}

 


 

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值