创建一个php文件,代码如下:
<?php //打开MySQL $link=mysqli_connect("127.0.0.1","root","root"); //选择数据库goods mysqli_select_db($link,"goods"); //查询goods表 $sql = "select * from goods"; //执行sql语句 $res = mysqli_query($link,$sql); $arr = []; //mysqli_fetch_assoc 从结果集中取得一行作为关联数组 while ($i = mysqli_fetch_assoc($res)) { $arr[] = $i; } //拼接 $str ="ID\t名称\t上架\t精品\t新品\n"; //表头 foreach ($arr as $val){ $str .= $val['id']."\t".$val['name']."\t".$val['show']."\t".$val['good']."\t".$val['new']."\n"; //内容 } //php 网页直接打开 excel header('Content-type: application/excel'); //定义excel文件名 header('Content-Disposition: attachment; filename="goods.xlsx"'); //输出后自动导出excel表格 echo $str;
运行此文件,将导出excel表,表里存放的是查询到的内容,即goods表的所有数据