php简单的将数据输出到excel表格

时间:2024-03-07 21:57:57

本例是php原生语法简单地将数据库中的内容导入到excel表格,没有使用php操作excel的插件,功能很是有限,存在的问题是无法指定表格的宽度,如果某个字段的数据比较多的情况下会出现字溢出表格的情况,如果要求比较高,建议用专门的插件操作数据。

<?php
header("Content-Type:text/html;   charset=utf-8"); 
Header( "Content-type:   application/octet-stream "); 
Header( "Accept-Ranges:   bytes "); 
Header( "Content-type:application/vnd.ms-excel ");   
Header( "Content-Disposition:attachment;filename=test.xls ");
$con = mysql_connect("localhost","root","root");
mysql_select_db("key");
mysql_query(\'set names utf8\');
if(isset($_POST[\'submit\'])){
    $pici=$_POST[\'pici\'];
    $sql = "select id,time,pici,skey from security_code where pici=$pici";
    }
    $result = mysql_query($sql,$con);
    echo "id\ttime\tpici\tskey";
    while ($rs=mysql_fetch_array($result)){
        echo "\n";
        echo $rs[\'id\']."\t".$rs[\'time\']."\t".$rs[\'pici\']."\t".$rs[\'skey\'];
    }