【问题标题】:Exporting data to excel with PHP使用 PHP 将数据导出到 Excel
【发布时间】:2020-12-18 20:50:32
【问题描述】:

我正在制作一个要求 2 个日期的简单表单,当用户输入并提交一个表格时,将显示该日期范围内的数据。 -> 这行得通

代码:

<!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>

我想将表中的数据导出到 Excel,但我似乎无法使其工作。我尝试将表格数据放入会话中,但是当我尝试单击导出到 excel 按钮时,它看起来就像只是擦除了数据。有人可以帮我解决这个问题吗?

亲切的问候

【问题讨论】:

    标签: php


    【解决方案1】:

    使用 PHP Excel 生成 Excel 文件。你可以在这里找到一个很好的 PHPExcel:https://github.com/PHPOffice/PHPExcel

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-09
      • 2012-10-25
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      • 1970-01-01
      相关资源
      最近更新 更多