注册 登录
编程论坛 PHP技术论坛

急要解决的问题

月光光2 发布于 2005-12-14 17:47, 984 次点击
php编程中怎样导出excel等格式文件?????
高手指引啊!!!!!!
6 回复
#2
wangshaobo2005-12-14 20:28

用以下代码就可以实现了:
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename=yourname.xls");
$fp=fopen("filename","r");
$result=fread($fp,filesize("filename");
echo $result;

#3
月光光22005-12-20 19:08
我是要用PHP导出MySql数据库的表格到excel文件
#4
月光光22005-12-20 19:17
可否讲具体一点???举一个例子吧
#5
wangshaobo2005-12-20 19:36
这就已经可以输出为excel文件了,你没有实验吗,还要怎么具体啊,你只要把里面的文件名修改成你的文件的名字就应该可以了
#6
月光光22005-12-23 10:47

例如具体的数据库,库里的数据表,都好像没有列出类似的信息!!!!

#7
wangshaobo2005-12-23 16:15
在网上找的,你可以参考一下:
如果是直接导出到EXCEL的话,依目前的技术能力似乎还不能做到。连大名鼎鼎的OpenOffice对EXCEL的支持也不是很好。
不过有种投机取巧的方法可以实现,就是从数据库中取出数据后,生成一个表格,样式自己定义好,然后让浏览器识别为下载excel。以下只是一个示例:
<?php
// 转载请注明phpteam
$title = "数据库名:test, 数据表:test, 备份日期:" . date("Y-m-d H:i:s");
$conn = @mysql_connect("localhost", "root", "") or die("不能连接数据库");
@mysql_select_db("test", $conn);
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=test.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo '<table border="1" cellspacing="2" cellpadding="2" width="50%" align="center">';
// 输出标题
echo '<tr bgcolor="#cccccc"><td colspan="3" align="center">' . $title . '</td></tr>';
$query = "select * from test";
$result = mysql_query($query) or die(mysql_error());
$fields = mysql_num_fields($result);
// 输出字段名
echo '<tr bgcolor="blue">';
for($i = 0; $i < $fields; $i++) {
echo '<td>' . mysql_field_name($result, $i) . '</td>';
}
echo '</tr>';
// 输出内容
while($row = mysql_fetch_row($result)) {
echo '<tr>';
for($i = 0; $i<$fields; $i++) {
echo '<td>' . $row[$i] . '</td>';
}
echo '</tr>';
}
echo '</table>';
?>
1