c# word转html(另存为)

c# word转html(另存为)

使用注意:

1.引用com类库:安装office后会有此类库,注意版本14.0是office2010版本:

 

方案一:

复制代码
 /// <summary>
        /// word另存为html
        /// </summary>
        /// <param name="filePath"></param>
        private void WordToHTML(string filePath)
        {
            var rootPath = AppDomain.CurrentDomain.BaseDirectory;
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            object nothing = Missing.Value;
            object newPath = string.Format(@"{0}File\html\{1}.html", rootPath, fileName);
            object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML;
            Microsoft.Office.Interop.Word.Application application = new Microsoft.Office.Interop.Word.ApplicationClass();
            Microsoft.Office.Interop.Word.Document document = application.Documents.Add(filePath, nothing, nothing);
            document.SaveAs2(newPath, format, nothing, nothing, nothing, nothing, nothing, nothing,
                nothing, nothing, nothing, Encoding.UTF8.CodePage, nothing, nothing, nothing);
            document.Close(nothing, nothing, nothing);
            application.Quit(nothing, nothing, nothing);
        }
复制代码

方案二:

复制代码
 /// <summary>
        /// word另存为html
        /// </summary>
        /// <param name="filePath"></param>
        private void WordToHTML2(string filePath)
        {
            var rootPath = AppDomain.CurrentDomain.BaseDirectory;
            var fileName = Path.GetFileNameWithoutExtension(filePath);
            object newPath = string.Format(@"{0}File\html\{1}.html", rootPath, fileName);
            Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.ApplicationClass();
            Type wordType = word.GetType();
            Microsoft.Office.Interop.Word.Documents docs = word.Documents;
            Type docsType = docs.GetType();
            Microsoft.Office.Interop.Word.Document doc = (Microsoft.Office.Interop.Word.Document)docsType.InvokeMember("Open",
            System.Reflection.BindingFlags.InvokeMethod, null, docs, new Object[] { filePath, true, true });
            Type docType = doc.GetType();
            docType.InvokeMember("SaveAs", System.Reflection.BindingFlags.InvokeMethod,
            null, doc, new object[] { newPath, Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatFilteredHTML });
            wordType.InvokeMember("Quit", System.Reflection.BindingFlags.InvokeMethod, null, word, null);
        }
复制代码

 

posted @   土豆哥  阅读(1439)  评论(0编辑  收藏  举报
编辑推荐:
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示