{"id":41,"date":"2010-09-22T09:33:39","date_gmt":"2010-09-22T14:33:39","guid":{"rendered":"http:\/\/unixmonkey.net\/?p=41"},"modified":"2010-09-22T09:33:39","modified_gmt":"2010-09-22T14:33:39","slug":"serving-html-tables-as-excel","status":"publish","type":"post","link":"https:\/\/unixmonkey.net\/?p=41","title":{"rendered":"Serving HTML tables as Excel"},"content":{"rendered":"<p>Did you know that Excel can open a plain-old HTML table file and treat it just like a native Microsoft XLS file?<\/p>\n<p>I&#8217;ve been doing this for ages, and its quite simple to pull off; just name your file something.xls and send the html with the content-type of &#8220;application\/vnd.ms-excel&#8221; (and even that is not strictly required).<\/p>\n<p>Even functions are somewhat supported like so.<\/p>\n<pre lang=html4strict><code>&lt;table&gt;\n  &lt;tr&gt;\n    &lt;td&gt;2&lt;\/td&gt;\n    &lt;td&gt;3&lt;\/td&gt;\n    &lt;td&gt;=sum(a1:b1)&lt;\/td&gt;\n  &lt;\/tr&gt;\n&lt;\/table&gt;<\/code><\/pre>\n<p>However, there are a few drawbacks to this worth sharing when trying to style the tables with CSS.<\/p>\n<p>1) Multiple class names are not supported:<\/p>\n<pre lang=html4strict><code>&lt;style type='text\/css'&gt;\n  td.foo { background-color: red; }\n&lt;\/style&gt;\n&lt;td class='foo bar'&gt;&lt;\/td&gt;<\/pre>\n<p><\/code><\/p>\n<p>Will not apply the background color. You have to use only a single class per element like so:<\/p>\n<pre lang=html4strict><code>&lt;style type='text\/css'&gt;\n  td.foo { background-color: red; }\n&lt;\/style&gt;\n&lt;td class='foo'&gt;&lt;\/td&gt;<\/code><\/pre>\n<p>2) Descendant selectors are not supported:<\/p>\n<pre lang=html4strict><code>&lt;style type='text\/css'&gt;\n  tr.odd td { background-color: red; }\n&lt;\/style&gt;\n&lt;tr class='odd'&gt;\n  &lt;td&gt;&lt;\/td&gt;\n&lt;\/tr&gt;<\/code><\/pre>\n<p>Will not apply the background color either. You will have to be very specific about the element you want to style.<\/p>\n<p>3) Certain colors and properties don't work:<br \/>\nIf you use a color like #ECECEC (a light gray), it will come out white, If you use #CCCCCC (a darker gray) it will work. Stick to the basics like #000000 (black) if you can.<\/p>\n<p>4) You'll have to add your styles either inline or in an html &lt;style&gt; block.<br \/>\nThis shouldn't come as a surprise, but linking to an external stylesheet won't work. Excel won't reach out to your server to style the content.<\/p>\n<p>5) Granularity will not be easily achieved:<br \/>\nStyling something with a 1px border is going to translate into a .5pt border. That's just how Excel rolls I guess.<\/p>\n<p>Try marking up an Excel workbook and using 'save as' -&gt; 'html' and opening up the file to checkout the markup Excel uses. This can give you some clues to what is acceptable.<\/p>\n<p>I hope these simple tips help someone in a similar situation, and keep them from using a heavy arcane library to generate excel files with some added styling.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Did you know that Excel can open a plain-old HTML table file and treat it just like a native Microsoft XLS file? I&#8217;ve been doing this for ages, and its quite simple to pull off; just name your file something.xls and send the html with the content-type of &#8220;application\/vnd.ms-excel&#8221; (and even that is not strictly [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[24,25,26,45],"_links":{"self":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/41"}],"collection":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":0,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"wp:attachment":[{"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unixmonkey.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}