【问题标题】:PHP export mysql data to Excel crontab linuxPHP 导出 mysql 数据到 Excel crontab linux
【发布时间】:2019-03-14 20:19:14
【问题描述】:

所以,我有这段代码可以将数据从 mysql 数据库导出到 excel 文件,我希望它与 crobjob 一起运行,问题是这段代码没有提取任何值,它只是给了我一个空的 scrren不知道为什么...

<?php  
//export.php  
$connect = mysqli_connect("localhost", "user", "pw", "bd");
$output = '';
if(isset($_POST["export"]))
{
    $query = "SELECT * FROM PersonalData ";
    $result = mysqli_query($connect, $query);
    if(mysqli_num_rows($result) > 0)
    {
        $output .= '
            <table class="table" bordered="1">  
                    <tr align="center">  
                        <th align="center">Family Name</th>  
                        <th align="center">First Name</th>  
                    </tr>';
        while($row = mysqli_fetch_array($result))
        {
            $output .= '
                    <tr>  
                       <td align="center">'.$row["FamilyName"].'</td>  
                       <td align="center">'.$row["FirstName"].'</td>  
                    </tr>';
        }
        $output .= '</table>';
        header('Content-Type: application/xls');
        header("Content-Type: application/force-download");
        header('Content-Disposition: attachment; filename=assets_colaboradores.xls');
        //
        echo $output;
    }
}
?>

【问题讨论】:

  • 如果它通过 cron 作业运行,它如何给你一个“空屏”?您是否考虑过您的条件之一没有得到满足?
  • ^^^ 模糊我的问题
  • 它给了我一个空屏幕,因为当我输入 php export.php 时它只输出一个空行。
  • 如果你想在 excel 中使用它,我建议你尝试使用 CSV 格式而不是 HTML,并给它一个 .csv 扩展名而不是 xls
  • 好吧,我设法让它输出一些东西,但是......它输出一切,html标签等作为纯文本

标签: php mysql excel cron


【解决方案1】:

尝试输出为 CSV。这是一个将关联数组转换为我构建的 CSV 的函数:

function array2CSV($fileName, $array, $headers = false) {

    // Set file headers
    header('Content-type: text/csv');
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    header('Pragma: no-cache');
    header('Expires: 0');

    // Open CSV to write
    $file = fopen('php://output', 'w');

    // Assign header names either from $headers or the array keys of $array
    $headers = ($headers) ? $headers : array_keys($array[0]);

    // Write the header row into the file
    fputcsv($file, $headers);
    foreach ($array as $row) {
        // Write a row to the file
        fputcsv($file, $row);
    }
    exit();
}

【讨论】:

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