export html to word Example

export html to word : Exporting a HTML page as a Microsoft word document can be done in different ways. “Export HTML to Microsoft Word”. How to Convert HTML to WORD?

export html to word

Today’s tutorial we are showing how to convert html to word format using simple Javascript. I have explained, how you can convert your html document into word .doc using JavaScript.

Microsoft Word or Doc (.doc) default format is ideal for exporting HTML data content in a file. first of all Click the “Choose Files” button to select your HTML files. and then Click the “Convert to WORD” button to start the conversion.

Also Read This 👉   java script alert yes no - How to create a dialog with “yes” and “no” options?

Export HTML to Word Document with JavaScript

Source HTML with Export button
index.html

<button onclick="ConvertExDoc('memberAllContentData');">Export as .doc</button>
<div id="memberAllContentData">
<h3>
        <center>Next Export HTML to MS Word Document using JavaScript</center>
    </h3>
    <h2>HTML to WORD Converter</h2>
    <p>Exporting a HTML page as a Microsoft word document can be done in different ways.</p>
<p>Convert your HTML files to Word online & free. 1. Select your HTML files from device, Google Drive, Dropbox to convert.n this article, I have explained, how you can convert your html document into word. DOC is a word processing file created by Microsoft.
    </p>
<div>
 

Don’t Miss : Convert HTML to Word or PDF

JavaScript Function to Export HTML Document

JavaScript code

<script>
function ConvertExDoc(element, filename = ''){
    var header = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word' xmlns='http://www.w3.org/TR/REC-html40'><head><meta charset='utf-8'><title>Export HTML to Word Document with JavaScript</title></head><body>";

    var footer = "</body></html>";

    var html = header+document.getElementById(element).innerHTML+footer;

    var blob = new Blob(['\ufeff', html], {
        type: 'application/msword'
    });
    
    // Specify link url
    var url = 'data:application/vnd.ms-word;charset=utf-8,' + encodeURIComponent(html);
    
    // Specify file name
    filename = filename?filename+'.docx':'document.docx';
    
    // Make download link element
    var filefulldlink = document.createElement("a");

    document.body.appendChild(filefulldlink);
    
    if(navigator.msSaveOrOpenBlob ){
        navigator.msSaveOrOpenBlob(blob, filename);
    }else{
        // Make a link to the file
        filefulldlink.href = url;
        
        // Setting the file name
        filefulldlink.download = filename;
        
        //triggering the function
        filefulldlink.click();
    }
    
    document.body.removeChild(filefulldlink);
}
</script>

<button onclick="ConvertExDoc('memberAllContentData');">Export as .doc</button>

<button onclick="ConvertExDoc('memberAllContentData','custom-file-name');">Export as .doc</button>

I hope you get an idea about export html to word.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.