Embed Excel Sheet in HTML
Introduction
In some use cases, we have a complicated Excel sheet to share on our personal website and we would like to make it interactive. Fortunately, Microsoft allows the user to do so via OneDrive and it supports both HTML iframe
and JavaScript.
In this blog post, I would like the discuss how to embed Excel sheet in HTML, the issues that I encountered, and how to fix the issues.
Embed Excel Sheet in HTML
Generate Code
Once the Excel sheet has been created or uploaded to Microsoft OneDrive, generating the Excel embed code could be as easy as clicking File → Share → Embed → Generate HTML.
Insert Code
Then we could copy the HTML iframe
code or the JavaScript code and insert it into our website.
Using the default HTML iframe
code will result in an embedded Excel sheet as follows. If you see lots of white spaces before the table, it is a bug from Microsoft and the bug will show in most of the browsers except the Brave browser.
1 | <iframe |
Using the default JavaScript code will result in an embedded Excel sheet as follows.
1 | <div id="myExcelDiv" style="width: 402px; height: 346px"></div> |
Fix JavaScript Issues
The width of the embedded Excel sheet seems to be a problem, but we could modify the width values in the style. A bigger problem is, unlike the iframe
embedding, the JavaScript embedding does not allow the user to input values and one HTML can only have one Excel sheet embedded (without problems) because the div id
is a pre-defined value myExcelDiv
.
Personally, I favor the JavaScript embedding over the HTML iframe
embedding. So we would like to try solving those problems on our own.
We noticed that there is an argument wdAllowInteractivity=0
in the src
for both JavaScript and iframe
, although it does not prevent the user from typing in iframe
, changing it to 1
allows the user to type successfully.
In addition, wdDivId=%22myExcelDiv%22
is the place for specifying custom div id
for embedding Excel sheets. We could use new div id
here.
Finally, we can also make the webpage opening a little bit faster by making the loading of JavaScript async
.
Fix Preview
The fixed JavaScript code is as follows. We could see that now the embedded Excel sheet looks pretty nice.
1 | <div id="myExcelDiv1" style="width: 100%; height: 400px"></div> |
Embed Excel Sheet in HTML