How to Export Data to Excel files Using PHP with tableExport.js


Technology and Entertainment > How to Export Data to Excel files Using PHP with tableExport.js
15/08/2024 18:00 484


In the previous article, we introduced how to import data from an Excel file using the phpoffice/phpspreadsheet library. Today, let’s look at how to export data from a table to an Excel file. Let’s get started!

For those who want to review how to import data from Excel, you can click this link to read the article How to Import Data from Excel with PHP using phpoffice/phpspreadsheet.

1. Install the library

  • You can download the file from the TableExport website TableExport.

2. Create a PHP file

  • Create a PHP file to test exporting data as an Excel file.
  • Declare a tag to call the TableExport library for further functions.
    <script type="text/javascript" src="css_js_xlsx/xlsx.core.min.js"></script>
    <script type="text/javascript" src="css_js_xlsx/FileSaver.min.js"></script>
    <script type="text/javascript" src="css_js_xlsx/tableExport.js"></script>

3. Write the table you want to export as data in the Excel file

  • In the body section, we will create a table with sample data.
<table id="id_table" >
    <tr>   
        <td>>First name/td>
        <td>>Last name/td>
        <td>>Sex/td>
        <td>>Mobile/td>
    </tr>
    <tr>
        <td>Mr. A</td>
        <td>AAA</td>
        <td>Male</td>
        <td>123456</td>
    </tr>
    <tr>
        <td>Mr. B</td>
        <td>BBB</td>
        <td>Male</td>
        <td>78910</td>
    </tr>
    <tr>
        <td>Mr. C</td>
        <td>CCC</td>
        <td>Female</td>
        <td>111213</td>
    </tr>
    <tr>
        <td>Mr. D</td>
        <td>DDD</td>
        <td>Male</td>
        <td>141516</td>
    </tr>
    <tr>
        <td>Mr. E</td>
        <td>EEE</td>
        <td>Female</td>
        <td>171819</td>
    </tr>
</table>

4. Call the export Excel function

  • Write a button to trigger the TableExport function to export the table data.
<button onClick="doExport()" >Click to export to Excel file</button>

<script>
function doExport() {
    $('#id_table').tableExport({
    type: 'excel',
    fileName: 'File_name_here',
    date: {html: 'mm/dd/yyyy'},
    mso: {
        fileFormat: 'xlsx',
    }
    });
}
</script>

Now combine all parts of the code together.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Export Excel File Data with PHP</title>


    <script> type="text/javascript" src="css_js_xlsx/xlsx.core.min.js"></script>
    <script> type="text/javascript" src="css_js_xlsx/FileSaver.min.js"></script>
    <script> type="text/javascript" src="css_js_xlsx/tableExport.js"></script>
 
</head>
<body>
 
    <button onClick="doExport()" >Click to export to Excel file</button>

    <table id="id_table" border="1" >
        <tr>
            <td>First name</td>
            <td>Last name</td>
            <td>Sex</td>
            <td>Mobile</td>
        </tr>
        <tr>
            <td>Mr. A</td>
            <td>AAA</td>
            <td>Male</td>
            <td>123456</td>
        </tr>
        <tr>
            <td>Mr. B</td>
            <td>BBB</td>
            <td>Male</td>
            <td>78910</td>
        </tr>
        <tr>
            <td>Mr. C</td>
            <td>CCC</td>
            <td>Female</td>
            <td>111213</td>
        </tr>
        <tr>
            <td>Mr. D</td>
            <td>DDD</td>
            <td>Male</td>
            <td>141516</td>
        </tr>
        <tr>
            <td>Mr. E</td>
            <td>EEE</td>
            <td>Female</td>
            <td>171819</td>
        </tr>
    </table>


    <script>>
        function doExport() {
            $('#id_table').tableExport({
            type: 'excel',
            fileName: 'File_name_here',
            date: {html: 'mm/dd/yyyy'},
            mso: {
                fileFormat: 'xlsx',
            }
            });
        }
    </script>


</body>
</html>


The result on the screen will allow you to export the data to an Excel file by simply pressing the export button. Try using this method, and there are additional features for customization. Check the TableExport website for more details.









Please rate your satisfaction with this article

Star 1 Star 2 Star 3 Star 4 Star 5

Post a Comment
Your email address will not be displayed to others. Required fields are marked *

CAPTCHA