Creating Excel file from MySQL

后端 未结 5 839
死守一世寂寞
死守一世寂寞 2021-02-09 08:42

I am trying to generate an XLS file from a table in a MySQL DB, but the Excel file is not properly formatted & given error when the Excel file generated \"The file which you

5条回答
  •  半阙折子戏
    2021-02-09 08:59

    I'm not sure about .xls but for outputting a MySQL result as a CSV table the fputcsv function does it without much fuss:

    // Clear any previous output
    ob_end_clean();
    // I assume you already have your $result
    $num_fields = mysql_num_fields($result);
    
    // Fetch MySQL result headers
    $headers = array();
    $headers[] = "[Row]";
    for ($i = 0; $i < $num_fields; $i++) {
        $headers[] = strtoupper(mysql_field_name($result , $i));
    }
    
    // Filename with current date
    $current_date = date("y/m/d");
    $filename = "MyFileName" . $current_date . ".csv";
    
    // Open php output stream and write headers
    $fp = fopen('php://output', 'w');
    if ($fp && $result) {
        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename='.$filename);
        header('Pragma: no-cache');
        header('Expires: 0');
        echo "Title of Your CSV File\n\n";
        // Write mysql headers to csv
        fputcsv($fp, $headers);
        $row_tally = 0;
        // Write mysql rows to csv
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
        $row_tally = $row_tally + 1;
        echo $row_tally.",";
            fputcsv($fp, array_values($row));
        }
        die;
    }
    

提交回复
热议问题