您现在的位置: 万盛学电脑网 >> 程序编程 >> 网络编程 >> php编程 >> 正文

php生成Excel文件 实现代码

作者:佚名    责任编辑:admin    更新时间:2022-06-22

 <p>有段日子没有更新博客了,生怕被百度遗忘啊,biu~biu~.最近有个项目需要统计网站的url和title,保存在excel里面,下面是具体的代码</p>

<pre class="php" name="code"><!--p

//php生成excel报表,是通过发送header()头信息完成的
header("Content-Type: application/vnd.ms-execl");
header("Content-Type: application/vnd.ms-excel; charset=gb2312");
//告知浏览器文件名称,并要求客户端下载
header("Content-Disposition:filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");

$link = mysql_connect('localhost', 'root', '') or die('Could not connect: ' . mysql_error());
mysql_select_db('novartis') or die('Could not select database');

mysql_query("SET NAMES gb2312");
$query = 'SELECT title,keywords,url,description FROM cms_content';
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
// 用 HTML 显示结果
//$str =mb_convert_encoding ("测试1","gb2312","utf-8");
echo "Titlet";
echo "Keywordst";
echo "Descriptiont";
echo "URLtn";
/* 格式: 换行:"tn": 单元格之间: "t" */

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $line['title']."t";
echo $line['keywords']."t";
echo $line['description']."t";
echo $line['url']."tn";

}


// 释放结果集
mysql_free_result($result);
// 关闭连接
mysql_close($link);
-->
</pre>
<div>&nbsp;</div>