使用.xlsx导出到Excel

时间:2019-05-21 17:32:47

标签: php mysql mysqli export-to-excel

我正在使用导出到Excel的脚本。该脚本以前使用mysqli和Excel扩展名.xls起作用。

现在,我正在尝试使用Excel扩展名.xlsx。尽管电子表格已导出为.xlsx文件,但工作表上没有任何数据。

代码如下:

<?php
include('../includes/database.php');

global $ts;
$ts = date('mdY-His');

$sql = "SELECT column1, column2, column3 FROM table WHERE column1 IS NOT NULL"; 
$stmt = $dbc->query($sql);

header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); 
header("Content-Disposition: attachment; filename=GRI&PSSReport_CustomerOnBoarding-".$ts.".xlsx");  
header("Pragma: no-cache"); 
header("Expires: 0");
header("Content-Transfer-Encoding: binary ");

/*******Start of Formatting for Excel*******/   
$sep = "\t"; //tabbed character
for ($i = 0; $i < $stmt->columnCount(); $i++) {
    $col = $stmt->getColumnMeta($i);
    //print_r($col);
    print_r($col['name'] . $sep);
}
print("\n");

//start while loop to get data
while($rows = $stmt->fetch(PDO::FETCH_NUM)){
    //print_r($rows);

    foreach($rows as $row){
        echo $row . $sep;

    }
    print("\n");
}   
?>

我不确定Excel工作表为何空白。该问题如何解决?

0 个答案:

没有答案