In this blog we will see how to convert HTML to Word or Excel and download in php with example. Converting html to word in this blog does not contain any third party library. 

Generate word and excel

Create a folder in XAMPP->htdocs and give appropriate name to it and create ConvertHTMLtoWord-Excel.php file in that folder and write below code in it.

ConvertHTMLtoWord-Excel.php

<html>

<head>

    <title>Convert HTML to Word and Excel</title>

</head>

<body>

<center>

<h1>Display Data</h1>

<form method="post">

    <table border="3" bgcolor="lightgrey">

        <tr>

            <th>Name</th>

            <th>Address</th>

            <th>Email</th>

            <th>Image</th>

            <th>Phone No</th>

        </tr>

        <tr>

            <td>Tarun</td>

            <td>Rander Road Surat</td>

            <td>tarunvaghela33@gmail.com</td>

            <td><img src="http://localhost/Demo/images/i1.jpg" height="100" width="100"></td>

            <td>9033363637</td>

        </tr>

        <?php

        if (!isset($_REQUEST["btnexcel"]) && !isset($_REQUEST["btndoc"]))

        {

?>

          <tr>

            <td colspan="5" align="center">        

                    <input type="submit" name="btnexcel" value="Generate Excel"> <input type="submit" name="btndoc" value="Generate Word">

            </td>

            </tr>

        <?php

        }

?>

    </table>

</form>

    <?php

    if (isset($_REQUEST["btnexcel"]))

    {

        header("Content-type: application/vnd.ms-excel");

        header("Content-Disposition: attachment;Filename=data.xls");

    }

    

    if (isset($_REQUEST["btndoc"]))

    {

        header("Content-Type: application/vnd.ms-word");

        header("content-disposition: attachment;filename=Report.doc");

    }

?>

</center>

</body>

</html>

Note: If you want to download excel file then click on Generate Excel button and if you want to download word file then click on Generate Word button. When you click on any button then file downloaded and you can see that file into download folder.  

Hope this blog will help you. Comment to ask query. Thank you..!!