MichaelSmith MichaelSmith - 18 days ago 5
HTML Question

Export HTML table to MSWord

I have an html file with a table that I'm converting to a .doc

I've been able to get the doc to open automatically in "print view"



The problem is the table is too big for the page. I've set the width to 100%, but word has the option "Allow tables to extend into margins" checked by default. I want to set it to "unchecked" in a similar way to how I'm setting the view to "print"

Here is the template I'm using:

<html xmlns:w="urn:schemas-microsoft-com:office:word">
<head>
<!--[if gte mso 9]>
<xml>
<w:WordDocument>
<w:View>Print</w:View>
<w:DoNotOptimizeForBrowser/>
</w:WordDocument>
</xml>
<![endif]-->
<style type="text/css">
<%= global_style %>
<%= report_style %>
</style>
</head>
<body>
<div id="word-export" class="WordSection1">


The docs mention something about a "w:GrowAutoFit", but if that would work, I'm not sure how to make it "falsey".



msdn docs

more msdn docs

Answer

Found it! You have to use w:Compatibility and w:DontGrowAutofit. Funny that DontGrowAutofit isn't referenced near the GrowAutoFit docs.

<html xmlns:w="urn:schemas-microsoft-com:office:word">
<head>
<!--[if gte mso 9]>
  <xml>
    <w:WordDocument>
      <w:View>Print</w:View>
      <w:DoNotOptimizeForBrowser/>
      <w:Compatibility>
        <w:DontGrowAutofit/>
      </w:Compatibility>
    </w:WordDocument>
  </xml>
<![endif]-->
<style type="text/css">
  <%= global_style %>
  <%= report_style %>
</style>
</head>
<body>
<div id="word-export" class="WordSection1">