使用微软的Microsoft.Office.Interop.Word组件也能将word转换为html,核心方法是调用Word的文档对象模型(DOM)来打开Word文档并另存为HTML格式,但这种方式需要程序所在电脑安装Microsoft Word软件,且运行程序的账户需要具有访问Word组件和文件路径的足够权限。同时确保调用Marshal.ReleaseComObject释放对象,否则可能导致Word进程无法彻底关闭,占用系统资源。
VS2022中通过添加Com引用添加Microsoft Word Object Library组件。

程序主要代码及转换后的html文件如下所示:
csharp
using Microsoft.Office.Interop.Word;
string inputFilePath = "测试输出文件.docx";
string outputFilePath = "testdoc.html";
Application wordApp = new Application();
Document wordDoc = null;
try
{
wordDoc = wordApp.Documents.Open(inputFilePath);
//wdFormatFilteredHTML格式去除了Word特有标签和样式,如果需要完整保留所有Word信息,
//应设置为wdFormatHTML
WdSaveFormat saveFormat = WdSaveFormat.wdFormatFilteredHTML;
wordDoc.SaveAs2(outputFilePath, saveFormat);
}
catch (Exception ex)
{
Console.WriteLine($"转换过程中出现错误: {ex.Message}");
throw;
}
finally
{
if (wordDoc != null)
{
wordDoc.Close(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordDoc);
}
wordApp.Quit(false);
System.Runtime.InteropServices.Marshal.ReleaseComObject(wordApp);
}

参考文献:
1\]https://blog.csdn.net/x1234w4321/article/details/140326650