通过table标签,PHP输出EXCEL的实现方法
网络编程 2021-07-05 09:49www.168986.cn编程入门
以下是利用table标签,对PHP输出EXCEL的实现代码进行了介绍,需要的朋友可以过来参考下
关键代码:
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
第一句是用来声明文件内容的格式;第二局是用来修改文件名的。如果没有第二个语句的话,生成的文件将是没有后缀名的。
实现代码:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">i love you</td>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr>
<td>1</td>
<td>test</td>
<td>20</td>
</tr>
<tr>
<td>2</td>
<td>test2</td>
<td>22</td>
</tr>
</table>
当然,我们很自然的想到了,是否可以把数据库的内容也通过这种方式输出到表格呢?
答案是可以的。
实现代码:
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=qianshou.xls");
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("SET NAMES GBK");
$query="select * from city ";
$r=mysql_query($query);
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">城市列表</td>
</tr>
<tr>
<td align="center">id</td>
<td align="center">p_id</td>
<td align="center">name</td>
</tr>
<?php
while($row=mysql_fetch_assoc($r)){
?>
<tr>
<td><?php echo $row[id] ?></td>
<td><?php echo $row[p_id] ?></td>
<td><?php echo $row[c_name]?></td>
</tr>
<?php
}
?>
</table>
代码如下:
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
第一句是用来声明文件内容的格式;第二局是用来修改文件名的。如果没有第二个语句的话,生成的文件将是没有后缀名的。
实现代码:
代码如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Conten-Disposition:filename=hp.xlsx");
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">i love you</td>
</tr>
<tr>
<td>编号</td>
<td>姓名</td>
<td>年龄</td>
</tr>
<tr>
<td>1</td>
<td>test</td>
<td>20</td>
</tr>
<tr>
<td>2</td>
<td>test2</td>
<td>22</td>
</tr>
</table>
当然,我们很自然的想到了,是否可以把数据库的内容也通过这种方式输出到表格呢?
答案是可以的。
实现代码:
代码如下:
<meta http-equiv="Content-Type" content="text/html; charset=gbk" />
<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:filename=qianshou.xls");
mysql_connect("localhost","root","");
mysql_select_db("test");
mysql_query("SET NAMES GBK");
$query="select * from city ";
$r=mysql_query($query);
?>
<table width="200" border="1">
<tr>
<td colspan="3" align="center">城市列表</td>
</tr>
<tr>
<td align="center">id</td>
<td align="center">p_id</td>
<td align="center">name</td>
</tr>
<?php
while($row=mysql_fetch_assoc($r)){
?>
<tr>
<td><?php echo $row[id] ?></td>
<td><?php echo $row[p_id] ?></td>
<td><?php echo $row[c_name]?></td>
</tr>
<?php
}
?>
</table>
编程语言
- 机械手焊接机器人编程 机械手焊接设备编程
- vb数据库编程实例,vb做数据库
- tiobe世界编程语言排行榜,全球编程语言排行
- 成都java编程培训 成都it编程培训中心
- 编程基础培训,编程基础知识培训
- flash游戏编程基础教程,flash游戏开发教程
- 不会英语能学编程吗 不会英语可以学程序员吗
- 爱心代码编程C语言 爱心代码编程c语言公式
- c语言编程学习入门 c语言编程快速入门
- 西门子plc编程培训 西门子plc编程培训班
- 哪里可以学编程 台州哪里可以学编程
- 少儿编程哪个好 少儿编程哪个品牌好
- 编程是什么意思,图形编程是什么意思
- c语言编程软件下载,c语言编程软件在哪下载
- java编程软件下载,java编程下载手机版
- ug编程培训学校,ug编程培训教程