Submit Post - Article - Ask a Question - Ask an Interview Question - Submit Useful Links

How to Export HTML Table to Excel

How to Export HTML Table to Excel?

By      20-Nov-2023    2

Solutions


Adil Shahbaz

Step 1: Add this cdn to your html page

<script src="//cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>

Step 2: Create a Table

<table id="myTable" class="table table-striped m-b-none">
                        <thead>
                           
                            <tr>
                                <th>Code</th>
                                <th>Name</th>
                                
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <td>1</td>
                            <td>Hello Pakistan</td>

                              </tr>
                        </tbody>
                    </table>

Step 3: Add this css to header of page

<style>
    .exportExcel {
        padding: 5px;
        border: 1px solid grey;
        margin: 5px;
        cursor: pointer;
        background-color: red;
    }
</style>

Step 4: Add this Script in bootom of page

<script>
    $(document).ready(function () {
        $('#myTable').DataTable({
            dom: 'Bfrtip',
            buttons: [
                'excelHtml5',
            ]
        });
    });
</script>

Thanks

Your Answer

10025
Submit Post - Article - Ask a Question - Ask an Interview Question - Submit Useful Links