导出特定的mysql表以在PHP中excel

时间:2016-06-28 19:39:33

标签: php mysql excel export

我必须将三个特定的表导出到Excel工作表,但是所有数据都被丢弃在一个字段中,造成了很大的混乱。是否可以导出三个表的内容并通过PHP在Excel中格式化它们?基本上我可以得到:

Date:            Subject             Participants
Start - End      Subject Title       User 1
                                     User 2
                                     User 3

基本上所以一切都是那样的?使其清晰可见(尽管这可能会导致更多参与者出现问题)。

另外,我会分享我的代码,也许有人可以看到我这次搞砸了什么。

    <div id="tableform">
        <form method="post">
            <table class="nostyle">
                <tr>
                    <th><label for="startDate">Begin datum:</label></th>
                    <td><input type="date" id="BeginDate" name="BeginDate" placeholder="DD-MM-YYYY"></td>
                    <td><input type="hidden" id="startDate" name="startDate" value="<?= $startdate; ?>"/></td>
                </tr>
                <tr>
                    <th><label for="endDate">Eind datum:</label></th>
                    <td><input type="date" id="EindDate" name="EindDate" placeholder="DD-MM-YYYY"></td>
                    <td><input type="hidden" id="endDate" name="endDate" value="<?= $enddate; ?>"/></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="generate" value="Jaarverslag genereren"/></td>
                </tr>
            </table>
        </form>
    </div>

功能:

<?php
public function generateReport($startDate = null, $endDate = null) {
    if (empty($startDate) && empty($endDate)) {
        $time = strtotime("-1 year", time());
        $startDate = date("Y-m-d", $time);
        $endDate = date("Y-m-d");
    }
    $this->generator->create('Jaaroverzicht-' . $startDate . '-' . $endDate);
    $data = array(
        "Datum" . '' . "Aanwezigen" . '' . "Bijscholing"
    );
    foreach ($this->dbSchooling->getYearReport($startDate, $endDate) as $row) {
        $users = "";
        $participants = $this->dbSchooling->getParticipants($row['bijscholing_id']);
        foreach ($participants as $participant) {
            $users .= $participant;
            if ($participant == end($participants)) {
                $users .= ', ';
            }
        }
        $myDateTime = \DateTime::createFromFormat('Y-m-d', $row['bijscholing_begindatum']);
        $newDateString = $myDateTime->format('d-m-Y');
        array_push($data, ($newDateString . "." . $users . "." . '.' . $row['bijscholing_titel']));
    }
    foreach ($data as $line) {
        $this->generator->store(explode('.', $line));
    }
    return $this->generator->close();
} ?>

1 个答案:

答案 0 :(得分:0)

代码已编辑为使用分号。

public function generateReport($startDate = null, $endDate = null) {
    if(empty($startDate) && empty($endDate)) {
        $time = strtotime("-1 year", time());
        $startDate = date("Y-m-d", $time);
        $endDate = date("Y-m-d");
    }
    /**
     * Title
     */
    $this->generator->create('Jaaroverzicht-'. $startDate . '-'. $endDate);
    $data = array(
        "Datum;Bijscholing;Instructeur;Aanwezigen"
    );
    /**
     * Kopstukken + inhoud
     */
    foreach($this->dbSchooling->getYearReport($startDate, $endDate) as $row) {
        $users = "";
        $participants = $this->dbSchooling->getParticipantsVoornaam($row['bijscholing_id']);
        foreach($participants as $participant) {
            $users .= $participant;
            if($participant !== end($participants)) {
                $users .= ', ';
            }
        }
        $myDateTime = \DateTime::createFromFormat('Y-m-d', $row['bijscholing_begindatum']);
        $newDateString = $myDateTime->format('d-m-Y');
        array_push($data, ($newDateString . ";" . $row['bijscholing_titel'] . " ; " . $row['bijscholing_presentator'] . ' ; ' . $users));
    }
    foreach ($data as $line)
    {
        $this->generator->store(explode(';', $line));
    }
    return $this->generator->close();
}

遇到的问题仅出现在特定版本的Excel中。该文件已在Open Office Calc中正确配置。通过使用分号作为分隔符,很容易弄清楚其余部分。希望这可以帮助其他有类似问题的人。