SQL查询结果写入Excel
好,所以我不知道如何做到这一点,除了安装PHPexcel
但是说我有一个数组查询的结果
$mysqli = new mysqli('server', 'user1', 'pass', 'users'); if ($mysqli->connect_error) die(connect error ('.$mysqli->connect_error.') '.$mysqli->connect_error); $date = date("Ymd"); $query = "SELECT * from usr WHERE date = '$date'"; while($row = $query->fetch_array()) { //EXCEL STUFF??? }
然后,我将如何写这个Excel电子表格? 任何帮助赞赏
第一:你有一个语法错误:
$mysqli = new mysqli('server', 'user1', 'pass', 'users'); if ($mysqli->connect_error) die('Connect Error: ' . $mysqli->connect_error); $date = date("Ymd"); $query = "SELECT * from usr WHERE date = '$date'"; while($row = $query->fetch_array()) { //EXCEL STUFF??? }
第二,你可以输出它作为一个普通的表,并添加到标题:
header("Content-Type: application/vnd.ms-excel; charset=utf-8"); header("Content-Disposition: attachment; filename=abc.xls"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
该脚本将自动允许您下载名为abc.xls
的文件与您的内容。
您可以使用PHPExcel写入或从Excel工作表读取。
链接: https : //github.com/PHPOffice/PHPExcel
这里是读取/写入Excel的示例代码
http://phpexcel.codeplex.com/wikipage?title=Examples
//来自网站的最小示例代码:
include 'PHPExcel/Writer/Excel2007.php'; // Create new PHPExcel object $objPHPExcel = new PHPExcel(); // Add some data $objPHPExcel->setActiveSheetIndex(0); $objPHPExcel->getActiveSheet()->SetCellValue('A1', 'Hello'); // Save Excel 2007 file $objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); $objWriter->save(str_replace('.php', '.xlsx', __FILE__));