如何使用JavaScript生成一个Word文档,并将页面方向更改为横向?(由纵向变为横向)

3
有没有可能用JavaScript创建一个包含两个不同方向页面(在同一个文档中)的Word文档?即第一页为纵向,第二页为横向?这正是我需要的。我已经尝试了很多方法,例如mso-break-type:section-break; mso-special-character:line-break; page-break-before:always;等等,但都没有成功。谢谢!以下是我目前的内容:
<script>
function export_to_word() {
   var link, blob, url;
   blob = new Blob(['\ufeff', document.getElementById("docx").innerHTML], {
         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);
 };
</script>

<html xmlns:office="urn:schemas-microsoft-com:office:office"
      xmlns:word="urn:schemas-microsoft-com:office:word"
      xmlns="http://www.w3.org/TR/REC-html40">

<br>
<button onclick="export_to_word()">Export</button>

<div id="docx">

    <style>
        @page portrait_A4_page {
            size:595.45pt 841.7pt;
            margin:1.0in 1.25in 1.0in 1.25in;
            mso-header-margin:.5in;
            mso-footer-margin:.5in;
            mso-paper-source:0;
        }

        div.portrait_A4_page { page:portrait_A4_page; }

        @page landscape_A4_page {
            size:841.7pt 595.45pt;
            mso-page-orientation: landscape;
            margin:1.25in 1.0in 1.25in 1.0in;
            mso-header-margin:.5in;
            mso-footer-margin:.5in;
            mso-paper-source:0;
       }

        div.landscape_A4_page { page:landscape_A4_page; }

    </style>


    <div class=portrait_A4_page>
        <p>standard A4 portrait page information</p>
    </div>    

    <div class=landscape_A4_page>
        <table border=1>
          <tr>
            <td>a table that goes really wide</td>  
          </tr>
        </table>
    </div>

</div>

可能是可行的,但我不确定使用你展示的方法……这似乎是Word的往返HTML文件格式?这是未经记录的,所以你最好在Word中创建一个简单的文档,包含你需要的功能,将其保存为完整的HTML格式,然后查看HTML以找到如何处理分节符以更改页面方向的方式。 - Cindy Meister
谢谢 - 你的解决方案有效! :) - Stacked Flowrider
1个回答

1

找到解决方案了!下面的代码可以在同一个由JavaScript生成的Word文档中从纵向(第一页)转换为横向(第二页)。它可能还可以进一步简化,但是这个代码有效:

<script>
function export_to_word() {
   var link, blob, url;
   blob = new Blob(['\ufeff', document.getElementById("docx").innerHTML], {
         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);
 };
</script>

<html xmlns:office="urn:schemas-microsoft-com:office:office"
      xmlns:word="urn:schemas-microsoft-com:office:word"
      xmlns="http://www.w3.org/TR/REC-html40">

<br>
<button onclick="export_to_word()">Export</button>

<div id="docx">

<style>
    table, tr, td, th{
        border: 1px solid black;
        border-collapse: collapse;
        padding: 5px;
        text-align: left;
    }

    .normal {
        font-family:"Calibri",sans-serif; 
        line-height:107%;
        font-size:11.0pt;
        mso-ascii-font-family:Calibri;
        mso-ascii-theme-font:minor-latin;
    }

    @page portrait_A4_page  {
        size:595.3pt 841.9pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;
        mso-header-margin:35.4pt;
        mso-footer-margin:35.4pt;
        mso-paper-source:0;
    }

    div.portrait_A4_page { page:portrait_A4_page; }

    @page landscape_A4_page {
        size:841.9pt 595.3pt;
        mso-page-orientation:landscape;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;
        mso-header-margin:35.45pt;
        mso-footer-margin:35.45pt;
        mso-paper-source:0;
    }

    div.landscape_A4_page { page:landscape_A4_page; }

</style>

<div class=portrait_A4_page>
  <span class=normal>
    <p>standard A4 portrait page information</p>
  </span>
    <br clear=all style='mso-special-character:line-break; page-break-before:always'>        
</div>    

<br clear=all style='page-break-before:always; mso-break-type:section-break'>

<div class=landscape_A4_page>
    <table class=normal>
        <tr>
            <td>a table that goes really wide</td>  
        </tr>
    </table>
</div>

</div>

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接