Tim's PHP Scripts

Excel to HTML

(exceltohtml)

This php script will convert an Excel .XLSX file to html and display the resultant code (including images) in a web page. It will recognise nearly all the formatting, images etc. in the original Excel XLSX document including most conditional formatting styles except Icon Sets and those based on formulas. If anyone needs support for any Conditional Formatting etc. not currently supported please contact me. Also at present it doesn't support graphs and charts.

The resultant HTML should look very much like the original.

PHP 5 or greater is needed. Will work on PHP 8.2 or later.

There is a test/demonstration page here where you can try this script out with one of your own Excel documents. Let me know if you see any issues or errors.


Latest version v1.0.3 provides the following enhancements over v1.0.2. -

  • A bug is cleared which prevented some images being displayed.
  • It now follows the original Excel spreadsheet as to whether gridlines are shown or not. There is also a new option to not show gridlines when they would otherwise be shown..
  • When in the 'Print' display mode, it will now just show the Excel defined 'Print Area' - assuming it is set.
  • In some environments a lot of warning messages were displayed. These have now been cleared.
  • An addition a few minor bugs have been cleared.

Features

  1. It replicates nearly all text formatting - font (so long as its a common font), font size, bold, italic, single and double underlining, superscript, subscript, strikethrough and colour. It will also support hyperlinks.
  2. It recognises and replicates virtually all number formatting, including %, scientific, currency, accountancy, time and date etc..
  3. It will recognise how numbers/text are positioned in a cell - left, centre, right - top, centre, bottom.
  4. It replicates most cell formatting including merged cells - background colour, border lines in solid, dotted and dashed in three thicknesses and the correct colour. It can recognise diagonal lines in all colours, but so far can only reproduce solid lines which are in only one thickness. At present it can't reproduce two diagonal lines to form a cross, so it reverts to a single diagonal line. It also can't replicate cell background patterns. (Currently no easy way to do these in CSS).
  5. It will reproduce most of the more common Conditional Formatting (either from a given number/string or the contents of a referenced cell):-
    Greater Than, Greater Than or Equal to, Less Than, Less Than or Equal to, Between, Not Between, Equal, Not Equal, Contains a Text String, Does Not Contain a Text String, Begins with a Text String, Ends with a Text String, Duplicate Values, Unique Values.
  6. It will now reproduce virtually all other conditional formatting except Icon Sets type and also any based on formulas. I.e. Data Bar, Colour Scale, Top N(N%), Bottom N(N%), Above/below average and 1,2,3 Standard Deviations Away.
  7. It will just display the area of populated spreadsheet cells. Any blank columns or rows, either before or after the populated cells are not shown. In the 'Print' mode it will display the Excel 'Print Area' - assuming that this has been set.
  8. It will display any images in the correct cell locations as per the spreadsheet. The images are also saved to the default folder 'images'. However this folder/directory name can be changed if desired.
  9. The results of all formulas/calculations are shown.
  10. It can display headers and footers together with any text formatting - selectable, see below.
  11. If the spreadsheet contains more that one populated sheet, then the default is to display them all in succession one after the other. However an option is provided to just display one, which can be selected by its sheet number.
  12. By default column widths and row heights are as per the original Excel spreadsheet. However 'Auto' mode is available as an option where the browser is allowed to choose them itself. This option can be useful for wide spreadsheets and/or narrower screens.
  13. If the default 'Print' option is selected, it will look similar to a printed Excel sheet following the 'Print Area' setting (if set), with headers and footers displayed, but no row and column references.
  14. If the 'Spreadsheet' option is selected then it will display the name of the sheet at the top together with the correct row and column references in the usual gray background along the top and left hand side as per a spreadsheet. Headers and footers will not be shown in this mode.
  15. It will follow the Excel spreadsheet as to whether gridlines are show or not. When gridlines would normally be shown there is an option to not display them.
  16. The resultant html code is designed to be used either as is, or (after saving) included in another html file.

Version v1.0.3 of this script is available for download from either:-

Github - https://github.com/timy352/exceltohtml

PHP Classes - https://www.phpclasses.org/package/13423-PHP-Converts-an-Excel-spreadsheet-to-HTML-.html

Versions

14 Jan 2025 - Version v1.0.3. A bug is cleared that prevented some images from being shown. Also it now follows the original Excel spreadsheet as to whether gridlines are shown or not. There is also a new option to not show gridlines when they would otherwise be shown. When in the 'Print' display mode, it will now just show the Excel defined 'Print Area' - assuming it is set. Also in some environments a lot of warning messages were displayed. These have now been cleared.

10 Jan 2025 - Version v1.0.2 removes a depreciated aspect of strpos.

10 Jan 2025 - Version v1.0.1 provides the following enhancements - It will now recognise more number formatting and will also replicate the following Conditional Formatting - Data Bar, Colour Scale, Top N(N%), Bottom N(N%), Above/below average and 1,2,3 Standard Deviations Away (except formula based ones). Also the clearance of a few bugs. It will also give better support to currency units - particularly trailing ones. In accounting formatting a '0' amount will be now be displayed as '-' as per Excel.

Version v1.0.0 - Original version.