C#word轉html 不開啟檔案轉換視窗
阿新 • • 發佈:2018-11-17
string fileName = System.IO.Path.GetFileNameWithoutExtension(file.FileName);
Word.Application word = new Word.Application();
Type wordType = word.GetType();
Word.Documents docs = word.Documents;
Type docsType = docs. GetType();
//第二個引數設定為false,不開啟word確認檔案轉換視窗,特別是在office2016的環境下
Word.Document doc = (Word.Document)docsType.InvokeMember("Open", System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { fullPath, false, true });
Type docType = doc.GetType();
if (Directory.Exists(Server.MapPath("~/html")) == false)
{
Directory.CreateDirectory(Server.MapPath("~/html"));
}
string strSaveFileName = Server.MapPath("~/html") + @"\" + fileName + ".html";
object saveFileName = (object) strSaveFileName;
docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod, null, doc, new object[] { saveFileName, Word.WdSaveFormat.wdFormatFilteredHTML });
docType.InvokeMember("Close", System.Reflection.BindingFlags.InvokeMethod, null, doc, null);
wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);