// 首先引入XLSXWriter包
include_once("XLSXWriter.php");
$writer = new XLSXWriter();
// 设置表头
$header = array(
'ID'=>'string',"IP"=>"string"
);
// 设置excel表
$sheet = 'Sheet1';
// 设置样式
$col_style = array_values($header);
$options = array(
'suppress_row'=>true, //suppress_row: 去掉会多出一行数据
'widths'=>array(
10,10 // 每一列的宽度
),
);
$writer->writeSheetHeader($sheet, $col_style, $options);
//写入第二行的数据,顺便指定样式
$header_style=array('height'=>32,'font-size'=>18,'font-style'=>'bold',
'halign'=>'left','valign'=>'center');
$member = date("Y-m-d H:i:s",$time).'>>>成功导出'.$count.'位客户';
$writer->writeSheetRow($sheet, array($member), $header_style);
/*设置标题头,指定样式*/
$styles1 = array(
'font'=>'宋体','font-size'=>10,'font-style'=>'bold', 'fill'=>'#eee',
'halign'=>'center', 'border'=>'left,right,top,bottom'
);
$title = array_keys($header);
$writer->writeSheetRow($sheet, $title, $styles1);
//执行xlsxwriter导出数据
if($data){
foreach($data as $row){
$writer->writeSheetRow('Sheet1', $row, array('height'=>16));
}
}
unset($data);
//重新刷新缓冲区,释放内存中的变量
ob_flush();
flush();
//合并单元格,第一行的大标题需要合并单元格
$writer->markMergedCell($sheet, $start_row=0, $start_col=0, $end_row=0, $end_col=27);
$filename = 'down/'.date('Ymdhms').'.xlsx';
$writer->writeToFile($filename);
echo "<a href='$filename'>点击下载</a>";
unset($writer);
exit(0);
需要XLSXWriter文件的小伙伴 可以点击 这里