简体   繁体   中英

Exporting data to excel with PHP

I'm making a simple form that asks for 2 dates, when inputted by the user and submitted a table will appear with the data in that date range. -> this works

code:

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>SWI report</title>
    <style>
        table {
            font-family: Arial, Helvetica, sans-serif;
            border-collapse: collapse;
            width: 100%;
        }

        td,
        th {
            border: 1px solid #ddd;
            padding: 8px;
        }

        tr:nth-child(even) {
            background-color: #f2f2f2;
        }

        tr:hover {
            background-color: #ddd;
        }

        th {
            padding-top: 12px;
            padding-bottom: 12px;
            text-align: left;
            background-color: #4CAF50;
            color: white;
        }
    </style>
</head>

<body>

    <form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        startdate: <input type="date" name="from_date">
        enddate: <input type="date" name="to_date">
        <input type="submit" name="date" id="date">
    </form>

    <div>
        <!-- excel export !-->
        <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
            <button type="submit" name="excel" value="excel" id='excel'> Export to excel</button>
        </form>
    </div>


    <?php
    require('settings.php');
    mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

    if (isset($_POST["date"])) {
        $startDate = date("Y-m-d", strtotime($_POST['from_date']));
        $endDate = date("Y-m-d", strtotime($_POST['to_date']));


        $sql = "SELECT latestv.* from(
        select distinct Werkomschrijving_nr from POH_GL4 where versie Between ? and ? ) changedw
        left join
        (select Werkomschrijving_nr, max(versie) AS maxdate, omschrijving from POH_GL4 
        group by Werkomschrijving_nr,omschrijving) latestv on latestv.Werkomschrijving_nr = changedw.Werkomschrijving_nr";

        $stmt = $db->prepare($sql);
        $stmt->execute([$startDate, $endDate]);
        $result = $stmt->fetchAll();

        echo "<table>";
        echo "<tr><th>nr werkomschrijving</th><th>Last change date </th><th>Omschrijving</th></tr>";

        foreach ($result as $key => $row) {

            echo "<tr>";
            echo "<td>" . $row['Werkomschrijving_nr'] . "</td>";
            echo "<td>" . $row['maxdate'] . "</td>";
            echo "<td>" . $row['omschrijving'] . "</td>";
            echo "</tr>";
        }

        echo "</table>";


        if (!empty($_POST)) {
            if (empty($result)) {
                echo '</br>';
                echo 'no results for this timeframe';
            }
        }
    

    }

    ?>

</body>

</html>

I want to export the data in the table to excel but I cant seem to make it work. I tried putting the table data in a session but when I try to click on the export to excel button it looks like it just wipes the data. can someone help me on this one?

kind regards

Use a PHP Excel for generatingExcel file. You can find a good one called PHPExcel here: https://github.com/PHPOffice/PHPExcel

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM