Excel to html with formatting

 

This spreadsheet contains the VBA code to write an Excel range as a clean, formatted HTML page, with optional auto-refresh.
NOTE If you click on the ‘This spreadsheet’ link above and it opens a ZIP file or a set of HTML files, right-click on the link and choose Save As.

Features:

  • Correctly renders all Excel formatting, including conditional formats.
  • Renders embedded charts.
  • Generates an HTML page with tabs identical to those in the Excel file.
  • Creates clean, formatted HTML that validates to HTML 4.01 strict.
  • Optimised stylesheet to ensure minimal page weight.
  • Supports merged cells with ROWSPAN and COLSPAN.
  • Updates the webpage automatically when cells’ values are changed.
  • The webpage automatically refreshes itself at a user-supplied interval.

The result is almost pixel-perfect, compare this Excel screenshot with the HTML page it generated.

14 March 2013. Update, improved lower tab bar to stop over-spilling (thanks to Paul Palmer) and added support for 64-bit Office.

21 August 2014. Many people have asked how to insert an image in the HTML page. It is not possible to get the contents of an embedded Excel image, but it is possible to get an embedded chart. Here’s how to perform this trick:

  1. In the sheet to be published, make sure no cells are selected
  2. Excel Menu->Insert->Chart and choose any chart type. This will insert an empty chart.
  3. Move and size the empty chart as required
  4. Select the chart by clicking its border
  5. Excel Menu->Layout and click the Picture icon
  6. Select an image file (JPG, BMP, etc) and Insert

28 September 2014. Now handles Unicode text correctly.

17 December 2018. Now outputs hyperlinks correctly.

Bookmark this app

Press Ctrl + D to add this page to your favorites or Esc to cancel the action.

Send the download link to

Send us your feedback

Oops! An error has occurred.

Invalid file, please ensure that uploading correct file

Error has been reported successfully.


You have successfully reported the error, You will get the notification email when error is fixed.

Click this link to visit the forums.

Immediately delete the uploaded & processed files.

Are you sure to delete the files?

Enter Url

Excel to HTML

I received an interesting question today – on how to easily publish an Excel file to a web page. Although there are a ton of ways to approach this problem (ranging from Excel Services in SharePoint to Excel Interop or ClosedXML) let us say we want to restrict to using only Excel and VBA. Printing Excel to HTML is a very useful feature if you want to publish your data/Workbook online.

The concept itself is very simple as HTML files are text files and therefore the problem is only to structure the data correctly in VBA before saving it to a HTML file.

I wanted to explore today 2 options:

  • Generating a HTML file via VBA
  • Generating a HTML file via the Publish feature in Excel

Both options are fairly useful ones – with the first one offering more flexibility and the second one being much easier to use.

Generating HTML via VBA

So lets start with the simple example of generating an Excel file from scratch just with VBA.

We have the following Excel table (ranging from A1 to C3):
Excel table

The Code

Sub Test()
    RangeToHtml Range("A1:C3"), "test.html"
End Sub

Sub RangeToHtml(rng As Range, fileName As String)
    Dim resBeg As String
    resBeg = "<html><head></head><body><table>"
    resEnd = "</table></body></html>"
    For i = 1 To rng.Rows.Count
        '---Rows---
        resBeg = resBeg & "<tr>"
        For j = 1 To rng.Columns.Count
            '---Columns---
            resBeg = resBeg & "<td>"
            resBeg = resBeg & rng.Cells(i, j).Value
            resBeg = resBeg & "</td>"
        Next j
        resBeg = resBeg & "</tr>"
    Next i
    Call SaveStringToFile(resBeg & resEnd, fileName)
End Sub

Sub SaveStringToFile(str As String, fileName As String)
    Open fileName For Output As #1
    Print #1, str
    Close #1
End Sub

Excel to HTML: The result

Lets see the result (actual HTML posted to this page):

Col1 Col2 Col3
1 2 3
4 5 6

Nothing extraordinary – just a very simple table without any formatting.
What the code does is traverse through the Excel Range replacing rows with the


tag and columns (or rather cells) with the
tag inserting the cell’s contents within. A very simple example.

Excel Publish to HTML feaure

The Publish to HTML feature is a neat capability that allows you to export your entire Workbook as a HTML web page through which you can easily navigate. You can easily Publish your Excel Workbook from VBA or directly from Excel.

To publish the Excel file go to Save As and select Publish to configure the publish options:
Publish to HTML Excel feature

Alternatively you can use the VBA code below to achieve the same:

 With ActiveWorkbook.PublishObjects.Add(xlSourceRange, _
        "PublishToHtml.htm", ";Sheet1", "$A$1:$C$4", _
        xlHtmlStatic, "PublishToHtml", "")
        .Publish (True)
 End With

Easy as that! The additional advantage is that the Publish to Excel feature will keep some of your formatting settings e.g. bold, italic fonts etc. Some, however, usually will be omitted e.g. borders.

Conclusions

Without resorting to external solutions there are least 2 easy options of generating html files from Excel. Personally I would prefer to have control over my HTML file and use VBA possibly adding my own css styles and formatting.

XLS (EXCEL) to HTML Converter

Convert your xls files to html online & free


Drop files here. 100 MB maximum file size or Sign Up

Convert to XLS

xls

Microsoft Excel Worksheet Sheet (97-2003)

XLS format is used to refer to the documents of the program Microsoft Excel. This file is a spreadsheet. All document data distributed and stored in the cells. Each cell has a unique address, which is denoted by letters and Arabic numerals. The cell can contain fixed data, and data in the form of formulas, which bind more cells.

HTML Converter

html

HyperText Markup Language

HTML is a Web format file. HTML source code can be changed in a text editor. HTML files are being developed for future use in the users web browser, allowing you to format text, images and other materials required sites. File with this format use tags to build web pages. HTML code is parsed Web browser and usually not visible to the user.

How to convert XLS to HTML

Step 1

Upload xls-file(s)

Select files from Computer, Google Drive, Dropbox, URL or by dragging it on the page.

Step 2

Choose «to html»

Choose html or any other format you need as a result (more than 200 formats supported)

Step 3

Download your html

Let the file convert and you can download your html file right afterwards

XLS to HTML Quality Rating

4.5 (889 votes)

You need to convert and download at least 1 file to provide feedback!

I have in Excel file with some cells with formatterd text (like colour and cartridge return). I need to export this text and display it into a web page, but I loose the format in the process.

Is it possible to convert the contents of the cells into HTML?

So for example a text in bold will be converted to ‘text in bold

And a carriage return will be converted to ‘1st line
2nd line’

Thanks

asked Nov 22, 2013 at 13:25

Selrac's user avatar

1

Using VBA in Excel it is possible to access the formatting of a cell and then write the appropriate HTML to build up a web page and then save that.
Or you can do a ‘Save As’ and select .htm as the file type. This will result in a web page that is chock full of MS formatting but it looks quite reasonable in FF and IE.

answered Nov 22, 2013 at 13:56

Dan Iveson's user avatar

Dan IvesonDan Iveson

9266 silver badges10 bronze badges

1

Понравилась статья? Поделить с друзьями:
  • Excel to google document
  • Excel to google contacts
  • Excel to exe portable
  • Excel to exe file
  • Excel to excel data connection