Home > php教程 > php手册 > php生成excel文件(csv)简单代码

php生成excel文件(csv)简单代码

WBOY
Release: 2016-05-25 16:38:11
Original
1083 people have browsed it

CSV是(逗号分隔值)的英文缩写,通常都是纯文本文件,建议使用WORDPAD或是记事本(NOTE)来开启,再则先另存新档后用EXCEL开启,也可以直接使用excel打开,和excel文件一样.

php生成excel文件(csv)参考代码如下:

<?php 
    header("Content-type:application/vnd.ms-excel"); 
    header("Content-Disposition:attachment;filename= www.phprm.com test_data.xls"); 
    //输出内容如下:  
    echo   "姓名"."t";  
    echo   "年龄"."t";  
    echo   "学历"."t";  
    echo   "n";  
    echo   "张三"."t";  
    echo   "25"."t";  
    echo   "本科"."t";
Copy after login


下面这种方法是从一个项目中摘取的(只支持1997-2003),t是Tab,代码如下:

<?php 
/** 
 * 导出 Excel 表格 
 * 
 */ 
public function excel(){ 
    // 表示输出的是excel文件 
   header("Content-type:application/vnd.ms-excel;"); 
    // 表示输出的文件名为lamp_dtype.xls 
   header("Content-Disposition:filename=lamp_dtype.xls"); 
    $exc .= "类型名称t宿舍类别t宿舍面积t容纳人数t月租/人t备注tn"; 
    $dtype = D(&#39;dtype&#39;); 
    // 查询数据库内的信息 
    $datas = $dtype->select(); 
    // 循环输出每条信息 
    foreach($datas as $data){ 
        $exc .="{$data[&#39;name&#39;]}t{$data[&#39;type&#39;]}t{$data[&#39;area&#39;]}t{$data[&#39;capacity&#39;]}t{$data[&#39;rent&#39;]}t{$data[&#39;remark&#39;]}tn"; 
    } 
    // UTF-8 转换成 GB2312 
    $exc =iconv("UTF-8","GB2312",$exc); 
    echo $exc; 
   // 避免输出下面的内容 
    exit(); 
}
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template