[[howto:web:serve-html-as-an-excel-file-from-a-web-application]]

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

howto:web:serve-html-as-an-excel-file-from-a-web-application [2018-08-05 04:25] (current)
Line 1: Line 1:
 +~~META:
 +creator = Brendan Kidwell
 +&date created = 2008-01-10
 +&date updated = 2014-08-30
 +~~
 +{{tag>​excel html web-development}}
 +~~DISCUSSION~~
 +====== Serve HTML as an Excel File from a Web Application ======
 +
 +It is possible to store HTML in a file named <wrap file>​.xls</​wrap>​ and then open that in Excel. This is useful for web applications that want to export data easily into a spreadsheet,​ as it's a lot easier than finding an Excel file handling library and programmatically creating an Excel binary file. This method allows you to:
 +
 +  * Output the spreadsheet as almost plain HTML, allowing for easy programming
 +  * Use the Freeze Panes feature to freeze header rows and/or columns
 +  * Use merged cells
 +  * Include line breaks in cells
 +
 +//Please note: The solution on this page shows you how to put HTML-encoded data in an <wrap file>​.xls</​wrap>​ file. This is speciically abusing the file extension, just to get Excel to try to open it; properly, an <wrap file>​.xls</​wrap>​ file must only have binary Excel 2000 format data in it. Recent versions of Microsoft Access **will** complain when you open the result. If you dismiss the warning, it should work fine.//
 +
 +//This is a quick-and-dirty way to get a table from your web app into Excel without an external library and without a lot of coding. Past comments about these deficiencies in the solution provided here have been deleted and future comments along those lines will be deleted.//
 +
 +If you don't like this solution, search for libraries and articles about writing valid <wrap file>​.xlsx</​wrap>​ files and [[http://​www.codeproject.com/​Articles/​38425/​How-to-Read-and-Write-ODF-ODS-Files-OpenDocument-S|<​wrap file>​.ods</​wrap>​ files]]. (The OpenDocument ODS file format is much simpler to get right than <wrap file>​.xlsx</​wrap>,​ and recent versions of Excel will happily open it without warning about the content not matching the filename.)
 +
 +===== Skeleton =====
 +
 +Create a new file with the <wrap file>​.xls</​wrap>​ extension and use this skeleton code to start:
 +
 +<file xml example.xls>​
 +<html xmlns:​o="​urn:​schemas-microsoft-com:​office:​office"​
 +xmlns:​x="​urn:​schemas-microsoft-com:​office:​excel"​
 +xmlns="​http://​www.w3.org/​TR/​REC-html40">​
 +
 +<​head>​
 +<meta http-equiv=Content-Type content="​text/​html;​ charset=windows-1252">​
 +<meta name=ProgId content=Excel.Sheet>​
 +<meta name=Generator content="​Microsoft Excel 11">​
 +
 +<!--[if gte mso 9]><​xml>​
 + <​x:​excelworkbook>​
 +  <​x:​excelworksheets>​
 +   <​x:​excelworksheet>​
 +    <​x:​name>​** WORKSHEET NAME **</​x:​name>​
 +    <​x:​worksheetoptions>​
 +     <​x:​selected></​x:​selected>​
 +     <​x:​freezepanes></​x:​freezepanes>​
 +     <​x:​frozennosplit></​x:​frozennosplit>​
 +     <​x:​splithorizontal>​** FROZEN ROWS + 1 **</​x:​splithorizontal>​
 +     <​x:​toprowbottompane>​** FROZEN ROWS + 1 **</​x:​toprowbottompane>​
 +     <​x:​splitvertical>​** FROZEN COLUMNS + 1 **</​x:​splitvertical>​
 +     <​x:​leftcolumnrightpane>​** FROZEN COLUMNS + 1**</​x:​leftcolumnrightpane>​
 +     <​x:​activepane>​0</​x:​activepane>​
 +     <​x:​panes>​
 +      <​x:​pane>​
 +       <​x:​number>​3</​x:​number>​
 +      </​x:​pane>​
 +      <​x:​pane>​
 +       <​x:​number>​1</​x:​number>​
 +      </​x:​pane>​
 +      <​x:​pane>​
 +       <​x:​number>​2</​x:​number>​
 +      </​x:​pane>​
 +      <​x:​pane>​
 +       <​x:​number>​0</​x:​number>​
 +      </​x:​pane>​
 +     </​x:​panes>​
 +     <​x:​protectcontents>​False</​x:​protectcontents>​
 +     <​x:​protectobjects>​False</​x:​protectobjects>​
 +     <​x:​protectscenarios>​False</​x:​protectscenarios>​
 +    </​x:​worksheetoptions>​
 +   </​x:​excelworksheet>​
 +  </​x:​excelworksheets>​
 +  <​x:​protectstructure>​False</​x:​protectstructure>​
 +  <​x:​protectwindows>​False</​x:​protectwindows>​
 + </​x:​excelworkbook>​
 +</​xml><​![endif]-->​
 +
 +</​head>​
 +<​body>​
 +
 +<​table>​
 +
 +<​thead>​
 +<tr>
 +   <​th>​** TABLE HEADER ROWS GO HERE **</​th>​
 +   ...
 +</tr>
 +</​thead>​
 +
 +<​tbody>​
 +<tr>
 +   <​td>​** TABLE DATA ROWS GO HERRE</​td>​
 +   ...
 +</tr>
 +...
 +</​tbody>​
 +
 +</​body>​
 +</​html>​
 +</​file>​
 +
 +If you are **not** using the Freeze Panes feature, omit the six lines from ''<​x:​freezepanes />''​ to ''</​x:​LeftColumnRightPane>''​.
 +
 +===== Making the File Available on the Web Server =====
 +
 +If the file is stored statically on the web server, make sure the server is configured to send the correct MIME type for the <wrap file>​.xls</​wrap>​ extention, which is ''​application/​msexcel''​.
 +
 +If you are generating the file dynamically as the output of a script, make sure to send this MIME type in the HTTP headers before your script outputs the body of the file. Also you should include the ''​Content-Disposition''​ header to instruct the browser to download the file and how to name it:
 +
 +<​code>​
 +Content-Type:​ application/​msexcel
 +Content-Disposition:​ attachment; filename=NAMEOFWORKSHEET.xls
 +</​code>​
 +===== Styling Cells =====
 +
 +Table header cells and data cells get certain default formatting (header: bold centered; data: normal left aligned). To add additional formatting, use the usual HTML tags like "​B",​ "​I",​ and "​SPAN"​. Hyperlinks may be embedded in cells.
 +
 +To add a **line break** within a cell, use this code:
 +
 +<code xml>
 +<br style="​mso-data-placement:​ same-cell;"​ />
 +</​code>​
 +
 +To **merge cells** together, do it as you normally would in an HTML table. Don't forget to include and omit the appropriate cells later in the row and in succeeding rows. For example, this code puts three cells in the a 2×2 block of cells:
 +
 +<code xml>
 +<tr>
 +   <​td rowspan="​2">​This cell is 2 rows high.
 +   <​td>​cell 2</​td>​
 +</tr>
 +<tr>
 +   <​td>​cell 3</​td>​
 +</tr>
 +</​code>​
 +===== Example File =====
 +
 +Here is an example file to play with: {{:​howto:​web:​serve-html-as-an-excel-file-from-a-web-application:​example-html.xls.zip?​direct}} (You can read and edit this file in a text editor!)
 +
 +Screenshot:
 +
 +{{:​howto:​web:​serve-html-as-an-excel-file-from-a-web-application:​example-html.xls.png?​direct&​600|}}