gpt4 book ai didi

php - 如何使用 php xlsxwriter 从 mysql 导出 xlsx

转载 作者:行者123 更新时间:2023-12-03 03:37:34 27 4
gpt4 key购买 nike

我使用PHP_XLSXWriter从 mysql 导出 xlsx 的类

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只有标题,没有数据,我的代码有什么问题?

最佳答案

最后,这些代码可以工作了。

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);

关于php - 如何使用 php xlsxwriter 从 mysql 导出 xlsx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43131215/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com