123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using Microsoft.Office.Interop.Word;
- using System.Runtime.InteropServices;
- namespace WordToHtml
- {
- class Program
- {
- static void Main(string[] args)
- {
- var result = "C:\\Users\\CrazyIter\\Desktop\\导入异常试卷\\IES5-109指考最前線-化學科.docx";
- //var result = "C:\\Users\\CrazyIter\\Desktop\\IES5-109指考最前線-化學科.docx";
- //Word 转 Html
- //前提:请先引用 Microsoft.Office.Interop.Word
- ApplicationClass w_app = new ApplicationClass();
- Type wordType = w_app.GetType();
- Documents w_docs = w_app.Documents;
- Type docsType = w_docs.GetType();
- object p_file_nm = result; //请在d分区下先新建好这个文件
- object saveFileName = @"C:\Users\CrazyIter\Desktop\导入异常试卷\1111111\abcd.html";
- Document w_doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, w_docs, new Object[] { p_file_nm, true, true }); new UnknownWrapper(null);
- Type docType = w_doc.GetType();
- docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, w_doc, new object[] { saveFileName, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
- wordType.InvokeMember("Quit",
- System.Reflection.BindingFlags.InvokeMethod,
- null,
- w_app,
- null
- );
- }
- /// <summary>
- /// word转成html
- /// </summary>
- /// <param name="wordFileName"></param>
- private string WordToHtml(object wordFileName)
- {
- //在此处放置用户代码以初始化页面
- Microsoft.Office.Interop.Word.ApplicationClass word = new Microsoft.Office.Interop.Word.ApplicationClass();
- Type wordType = word.GetType();
- Documents docs = word.Documents;
- //打开文件
- Type docsType = docs.GetType();
- Document doc = (Document)docsType.InvokeMember("Open",
- System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { wordFileName, true, true });
- //转换格式,另存为
- Type docType = doc.GetType();
- string wordSaveFileName = wordFileName.ToString();
- string strSaveFileName = wordSaveFileName.Substring(0, wordSaveFileName.Length - 3) ;
- object saveFileName = (object)strSaveFileName;
- docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, new object[] { saveFileName, WdSaveFormat.wdFormatFilteredHTML });
- docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod,
- null, doc, null);
- //退出 Word
- wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod,
- null, word, null);
- return saveFileName.ToString();
- }
- }
- }
|