Saving Excel files with HTML tables - quirks

in html, tables, PHP, excel, style, formatting

Sometimes you need to save data from your webpage to an excel file. Though there are many libraries that can accomplish that the "proper" way, it is much faster (and easier!) to just output an HTML table to a file with the "xls" extension. However, there are a couple of things to note to make theming look as it should!

  • First and foremost, you CAN use classes and styles, as long as they are inline ( ie you have them in the document <head> and not linked to a file. If using PHP, you can just ouptut a <style> element right in the <body> without any issues. Don't inline everything to the element level, it's a pain!
  • All text is formatted via pt units, not px ( eg font-size: 11pt )
  • Border width is VERY quirky if explicitly (numerically) defined - use text aliases for width such as thin, medium, thick ( eg border: thin solid black )
  • Enforcing specific types of data ( eg cell content is a number, or text ) can be done via a proprietary style -- see here for a long list ( eg  mso-number-format:"\@" will make the cell to be text )
  • Alternatively, you can add the zero-width character to force a numeric cell to type text -- just prepend &#8203; to the cell content. It is a non-visibile, non-pritable character that will force Excel to consider the cell as text.

I hope the above help others, since it took a LONG time to get something that should be simple done.