Home > php教程 > php手册 > php输出excel的简单方法(csv格式)

php输出excel的简单方法(csv格式)

WBOY
Release: 2016-05-25 16:48:48
Original
1348 people have browsed it

在php中有专业的excel处理类我们可以用它来增加,删除或处理excel中相关数据,如果我们只是要生成excel就可以使用下面方法简单又快捷。

如果不读数据我们直接使用下面代码就可以了,只需要把相关的数据导出到excel表就可以了,这么简单的操作就不需要用那些类库什么的了。直接用header的方式就可以了:header("Content-type:application/vnd.ms-excel");

实例代码如下:

<?php
header("Content-type:application/vnd.ms-excel");
header(&#39;Content-Disposition: attachment;filename="yingcai.xls"&#39;);
?>
<table border="1"> 
<tr> 
<td>用户名</td> 
<td>密码</td> 
</tr> 
<tr> 
<td>admin</td> 
<td>yingcai</td> 
</tr> 
</table>
Copy after login

简单纯粹的php导出excel表格方法,木有过多的解释,花哨的装饰,一看就懂,实例代码如下:

<?php
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment; filename=用户信息表.xls");
require_once (&#39;./class.db.php&#39;);
$db = new DB(&#39;localhost&#39;, &#39;app_vmovies&#39;, &#39;root&#39;, &#39;123456&#39;);
$sql = &#39;select * from `user` order by `id` desc&#39;;
$ret = $db->get_results($sql);
// 输出内容如下:
echo "ID" . "\t";
echo "昵称" . "\t";
echo "性别" . "\t";
echo "OpenId" . "\t";
echo "平台" . "\t";
echo "注册日期" . "\t";
echo "\n";
$len = count($ret);
for ($i = 0; $i < $len; $i++) {
    echo $ret[$i]["id"] . "\t";
    echo $ret[$i]["nickname"] . "\t";
    echo $ret[$i]["gender"] . "\t";
    echo $ret[$i]["openid"] . "\t";
    echo $ret[$i]["pf"] . "\t";
    echo $ret[$i]["regdate"] . "\t";
    echo "\n";
}
?>
Copy after login

如果程序是utf-8编码的,还需要用iconv函数去转码,不然是会乱码的,乱码的,另word格式导入类似,指定header就可以了,实例代码如下:

header("Content-Type:   application/msword");         
header("Content-Disposition:   attachment;   filename=doc.doc");
Copy after login

               
               

文章链接:

随便收藏,请保留本文地址!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template