Как экспортировать xlsx из mysql с помощью php xlsxwriter

Я использую класс PHP_XLSXWriter для экспорта xlsx из mysql

include_once('xlsxwriter.class.php');
$filename = "example.xlsx";
header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
header('Content-Transfer-Encoding: binary');
header('Cache-Control: must-revalidate');
header('Pragma: public');   
$query="my query here";
$result = mysql_query($query); 
$rows = mysql_fetch_assoc($result); 
$header = array(
  'ID'=>'integer',
  'Subject'=>'string',
  'Content'=>'string',
);
$writer = new XLSXWriter();
$writer->writeSheetHeader('Sheet1', $header);
foreach($rows as $row)
    $writer->writeSheetRow('Sheet1', $row);
//$writer->writeSheet($rows,'Sheet1', $header);//or write the whole sheet in 1 call 
$writer->writeToStdOut();
//$writer->writeToFile('example.xlsx');
//echo $writer->writeToString();
exit(0);

экспорт xlsx имеет только заголовок, без данных, в чем проблема моего кода?

2 ответа

Решение

Наконец, эти коды работают.

include_once('xlsxwriter.class.php');
    $filename = "example.xlsx";
    header('Content-disposition: attachment; filename="'.XLSXWriter::sanitize_filename($filename).'"');
    header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    header('Content-Transfer-Encoding: binary');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');   
    $query="my query here";
    $result = mysql_query($query); 
    $rows = mysql_fetch_assoc($result); 
    $header = array(
      'ID'=>'integer',
      'Subject'=>'string',
      'Content'=>'string',
    );
    $writer = new XLSXWriter();
    $writer->writeSheetHeader('Sheet1', $header);
    $array = array();
    while ($row=mysql_fetch_row($result))
    {
        for ($i=0; $i<mysql_num_fields($result); $i++ )
        {
        $array[$i] = $row[$i];
        //$array[$i] = strip_tag($row[$i],"<p> <b> <br> <a> <img>");
        }
        $writer->writeSheetRow('Sheet1', $array);
    };

    //$writer->writeSheet($array,'Sheet1', $header);//or write the whole sheet in 1 call    

    $writer->writeToStdOut();
    //$writer->writeToFile('example.xlsx');
    //echo $writer->writeToString();
    exit(0);

попробуйте этот https://github.com/WeiB71/table2excel

      function exportTableToExcel(tableName,fileName) {
    var table2excel = new Table2Excel();
    table2excel.export($("#" + tableName), fileName);
}
Другие вопросы по тегам