【问题标题】:export html table as word file and change file orientation将 html 表格导出为 word 文件并更改文件方向
【发布时间】:2016-07-19 18:09:49
【问题描述】:

我有将 html 表导出到 word 文件的 jquery 函数。功能很好用,但我需要将 word 文件旋转到横向。有人可以帮帮我吗?

这里是js函数:

    <SCRIPT type="text/javascript">
$(document).ready(function () {
    $("#btnExport").click(function () {
        var htmltable= document.getElementById('tblExport');
        var html = htmltable.outerHTML;
        window.open('data:application/msword,' + '\uFEFF' + encodeURIComponent(html));
    });
});
  Response.AddHeader("Content-Disposition", "attachment;filename=myfilename.docx");

【问题讨论】:

  • 不要认为这是可能的,因为您不是从头开始创建 word 文档,而是将 HTML 内容作为 word 文档发送到响应。
  • @Nimesh - 如果您创建与 Word 兼容的 HTML 并使用 MS Office CSS 样式,这是可能的。
  • @Martynas 先生,您是否找到将.doc 文件导出为portraitlandscapeÀ4 的最终解决方案,因为当我打开文件时,下面的答案位于webpage layout那么我该如何解决这个问题呢?

标签: javascript jquery html ms-word export


【解决方案1】:

将 HTML 导出到 Microsoft Word

您可以通过在导出的 HTML 中包含 CSS 来设置页面方向、纸张大小和许多其他属性。有关可用样式的列表,请参阅MS Office prefixed style properties 某些样式具有依赖关系。例如,要设置 mso-page-orientation,您还必须设置页面大小,如下面的代码所示。

更新:
在 FireFox、Chrome、Opera、IE10-11 中使用 Word 2010-2013 进行测试。对 Chrome 和 IE10 进行小的代码更改。不适用于缺少 window.Blob 对象的旧版浏览器 (IEhttps://stackoverflow.com/a/10195751/943435

     @page WordSection1{
         mso-page-orientation: landscape;
         size: 841.95pt 595.35pt; /* EU A4 */
         /* size:11.0in 8.5in; */ /* US Letter */
     }
     div.WordSection1 {
         page: WordSection1;
     }

要查看完整的工作演示,请参阅:https://jsfiddle.net/78xa14vz/3/

用于导出到 Word 的 Javascript:

 function export2Word( element ) {

   var html, link, blob, url, css;

   css = (
     '<style>' +
     '@page WordSection1{size: 841.95pt 595.35pt;mso-page-orientation: landscape;}' +
     'div.WordSection1 {page: WordSection1;}' +
     '</style>'
   );

   html = element.innerHTML;
   blob = new Blob(['\ufeff', css + html], {
     type: 'application/msword'
   });
   url = URL.createObjectURL(blob);
   link = document.createElement('A');
   link.href = url;
   link.download = 'Document';  // default name without extension 
   document.body.appendChild(link);
   if (navigator.msSaveOrOpenBlob ) navigator.msSaveOrOpenBlob( blob, 'Document.doc'); // IE10-11
       else link.click();  // other browsers
   document.body.removeChild(link);
 };

还有 HTML:

<button onclick="export2Word(window.docx)">Export</button>
<div id="docx">
  <div class="WordSection1">

     <!-- The html you want to export goes here -->

  </div>
</div>

【讨论】:

  • 它工作得很好,但即使我将 blob 转换为 base64,我也没有文件扩展名(我需要 base64 而不是即时下载)。你能帮我吗????
  • @churnsolidet - 这有帮助吗? example
  • @Roberto 它不适用于图像...看看这个 jsfiddle jsfiddle.net/shamoh19/9zhgnyek
  • @ShaMoh - 不幸的是,Word 不接受数据 uri 图像。最简单的解决方案是将图像 src 设置为实际的 url 图像源(jpg、gif、png)。此页面可能会有所帮助:Inserting a base64 encoded image into a Word 2016 document
  • 罗伯托,你太棒了。谢谢你。很高兴您也提供了非 jQuery 解决方案。给任何使用它的人的提示 - 它有助于使文件名变得有用。 link.download = '测验结果'; if (navigator.msSaveOrOpenBlob) navigator.msSaveOrOpenBlob( blob, 'QuizResults.doc'); // IE10-11
猜你喜欢
  • 2020-04-26
  • 2016-09-26
  • 2014-10-01
  • 1970-01-01
  • 2011-11-01
  • 1970-01-01
  • 2020-02-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多