隐藏

c# word转为html的方法(不需安装office)

发布:2020/1/15 16:31:17作者:管理员 来源:本站 浏览次数:997

在c#中,比较常用的word文件转为html的方法是采用Microsoft.Office.Interop.Word包,但是使用这种方法需要在服务器上安装office。

翻看github找到了一种不需要安装office,并且总体效果还算不错的方法,Mammoth

在Nuget上进行安装

Install-Package Mammoth

使用方法很简单

    using Mammoth;
     
    var converter = new DocumentConverter();
    var result = converter.ConvertToHtml("document.docx"); //参数为想要转化的文档
    var html = result.Value; // The generated HTML
    var warnings = result.Warnings; // Any warnings during conversion

存在不尽如人意之处:

(1)忽略了一些细节,如字体,文本大小,字体颜色等。

(2)表格本身的格式(例如边框)当前被忽略,但文本的格式与文档的其余部分相同。

github地址:https://github.com/mwilliamson/dotnet-mammoth