PHP导出excel(*.xls)文件的简单方法

 
大家都知道phpMyAdmin能够导出excel格式的文件,而实际上它只是导出了*.csv格式的文件,而不是真正的excel格式(*.xls)。如果你想得到真正的excel文件格式,也就是*.xls,你可以参看下面的代码!
1. 定义函数 
 程序代码
function xlsBOF() { 
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);  
    return; 


function xlsEOF() { 
    echo pack("ss", 0x0A, 0x00); 
    return; 


function xlsWriteNumber($Row, $Col, $Value) { 
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0); 
    echo pack("d", $Value); 
    return; 


function xlsWriteLabel($Row, $Col, $Value ) { 
    $L = strlen($Value); 
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L); 
    echo $Value; 
return; 


2、主程序
 程序代码
   // 查询数据库
    $result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")

    // 文件头
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=$courseid-$sec.xls "); 
    header("Content-Transfer-Encoding: binary ");

    // 向表中添加数据

                xlsBOF(); 
                xlsWriteLabel(1,0,"Student Register $semester/$year");
                xlsWriteLabel(2,0,"COURSENO : ");
                xlsWriteLabel(2,1,"$courseid");
                xlsWriteLabel(3,0,"TITLE : ");
                xlsWriteLabel(3,1,"$title");
                xlsWriteLabel(4,0,"SETION : ");
                xlsWriteLabel(4,1,"$sec");
                xlsWriteLabel(6,0,"NO");
                xlsWriteLabel(6,1,"ID");
                xlsWriteLabel(6,2,"Gender");
                xlsWriteLabel(6,3,"Name");
                xlsWriteLabel(6,4,"Lastname");
                $xlsRow = 7;
                while(list($id,$prename,$name,$sname,$grade)=mysql_fetch_row($result)) {
                    ++$i;
                          xlsWriteNumber($xlsRow,0,"$i");
                          xlsWriteNumber($xlsRow,1,"$id");
                          xlsWriteLabel($xlsRow,2,"$prename");
                          xlsWriteLabel($xlsRow,3,"$name");
                          xlsWriteLabel($xlsRow,4,"$sname");
                    $xlsRow++;
                    }
                     xlsEOF();
                 exit();

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值