将Word转换为HTML然后在网页上呈现HTML

Jam*_*son 9 c# ms-word

我的管道中有一个艰难的项目,我不知道从哪里开始.我的老板希望能够以HTML格式显示Word文档,它看起来与word文档相同.

经过一段时间的尝试让我在弹出窗口或灯箱中显示word文档后,他不得不剥离将其转换为HTML的单词内容,将其保存在数据库中,然后在网页上将其显示为HTML .

你们可以给我一些好的弹药,如果显示word文档更好(不那么繁琐,更少的存储空间更安全等).

或者,如果将单词文档转换为HTML方式很容易让我这样做.

我目前拥有的技术是实体框架,LINQ,MVC,C#,Razor.

我们目前使用HTmlAgilityPack,但这会删除所有格式,并且不允许文档显示得很好.

Dav*_*ish 7

我们使用http://www.aspose.com/(我认为我们使用的是Aspose词)来执行类似的任务,并且它工作得很好.(涉及成本)

我建议转换为HTML会给文档带来最糟糕的再现.我们使用的一个解决方案是生成文档的Jpeg图像并显示它.

如果您需要能够执行查找和复制/粘贴文本等操作 - 我建议将文档转换为.pdf,并在客户端计算机安装的任何标准pdf查看器中内联显示.


小智 6

如果您使用的是DOCX,您可以使用Microsoft提供的Open XML SDK,它非常易于使用和清理.从MSDN获取的样本

// This example shows the simplest conversion. No images are converted.
// A cascading style sheet is not used.
byte[] byteArray = File.ReadAllBytes("Test.docx");
using (MemoryStream memoryStream = new MemoryStream())
{
    memoryStream.Write(byteArray, 0, byteArray.Length);
    using (WordprocessingDocument doc =         WordprocessingDocument.Open(memoryStream, true))
    {
        HtmlConverterSettings settings = new HtmlConverterSettings()
        {
            PageTitle = "My Page Title"
        };
        XElement html = HtmlConverter.ConvertToHtml(doc, settings);

        // Note: the XHTML returned by ConvertToHtmlTransform contains objects of type
        // XEntity. PtOpenXmlUtil.cs defines the XEntity class. See
        // http://blogs.msdn.com/ericwhite/archive/2010/01/21/writing-entity-references-using-linq-to-xml.aspx
        // for detailed explanation.
        //
        // If you further transform the XML tree returned by ConvertToHtmlTransform, you
        // must do it correctly, or entities do not serialize properly.

        File.WriteAllText("Test.html", html.ToStringNewLineOnAttributes());
    }
}
Run Code Online (Sandbox Code Playgroud)

您可能还想查看Word自动化服务http://blogs.office.com/b/microsoft-word/archive/2009/12/16/word-automation-services_3a00_-what-it-does.aspx