对于如何将各种电子文本的图书进行预处理,昨天晚上想了一下,想到先将文本转化为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);
}
}
}