【问题标题】:Is there a tool or library, similar to wkhtmltopdf, to convert html to word documents?有没有类似于 wkhtmltopdf 的工具或库,可以将 html 转换为 word 文档?
【发布时间】:2012-01-04 14:39:48
【问题描述】:

它必须能够处理 css 和 javascript,因为页面包含“highcharts”javascript 图表。

它不必是一个外部进程,一个库也很棒。

基本上,我需要能够创建一个包含高图作为图像以及一些文本和其他图形的 Word 文档。

任何想法都会很棒。

【问题讨论】:

  • 如果您使用 VisualStudio,您可以在 C# 中执行此操作,请查看 Microsoft.Interop 服务

标签: c# asp.net asp.net-mvc ms-word


【解决方案1】:

以这个为例

这在很大程度上取决于您尝试转换的 html 文档的性质。一种简单的方法是使用 Word 自动化打开 .html 文档,然后将其保存为 .doc 文档。

        object readOnly = false;
        object isVisible = true;
        object missing = System.Reflection.Missing.Value; // Values we don't care about
        object fileName = "C:/webpage.htm";
        object newFileName = "C:/webpage.doc";       

        Microsoft.Office.Interop.Word.Application word = new Microsoft.Office.Interop.Word.Application();

        // word.Visible = true; // To see what's happening

        Microsoft.Office.Interop.Word.Document document = word.Documents.Open(ref fileName, ref missing, ref readOnly, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        document.Activate();

        object saveFormat = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocument;

        document.SaveAs(ref newFileName, ref saveFormat, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing, ref missing);

        document.Close(ref missing, ref missing, ref missing);

注意

  • 您必须添加对 Microsoft.Office.Interop.Word 或类似内容的引用
  • ref 缺失 参数的数量取决于您使用的 Word 版本
  • 您必须在文件名中使用完整路径,因为 Word 实例从系统文件夹开始。

【讨论】:

  • 谢谢,这会以 URL 作为输入吗? word 是否能够处理由 javascript 创建的 dom 元素?
  • 它在哪里显示 c:/webpage.htm 或任何 ext 被替换为 URL 的地方,你可能需要或可能不需要做一些 URL 编码.. 你将不得不对此进行试验你的用例是..这是让你开始或朝着正确的方向开始的东西..还要密切注意上面列出的注意事项
  • 我希望这会有所帮助......请随意投票
  • 无法渲染图表
  • 你有什么代码 .. 除非我看到你写的是什么,否则我不知道你在做什么 谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-27
  • 1970-01-01
  • 2011-04-27
  • 2022-10-25
相关资源
最近更新 更多