大家都知道 phpMyAdmin 可以把数据导出为 Excel 格式,但是phpMyAdmin 只能导出 .csv 格式的文件,不算真正的excel格式. 如果你对PHP的变成感兴趣,还需要导出真正的excel格式,那就好好看一下下面的了:

导出XLS格式的文件的PHP例子

1. 创建关于创建XLS文件的函数

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. 向浏览器发出文件头以下载文件.

   // Query Database
    
$result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")

    
// Send Header
    
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 ");

    
// 输出XLS 数据单元

    
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();

翻译:aslibra.com



原创内容如转载请注明:来自 阿权的书房
收藏本文到网摘
cjs Email
2006/11/29 17:48
laugh可惜就是xlsWriteLabel($xlsRow,$a,"$p");如果$p是中文,就会输出为0
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemotemotemotemotemotemotemotemotemot
emotemotemotemotemotemotemotemotemotemotemotemot
打开HTML 打开UBB 打开表情 隐藏
昵称   密码   游客无需密码
网址   电邮   [注册]
               

验证码 不区分大小写
 

阅读推荐

服务器相关推荐

开发相关推荐

应用软件推荐