Home > Backend Development > PHP Tutorial > Simple code to generate excel file (csv) in php_PHP tutorial

Simple code to generate excel file (csv) in php_PHP tutorial

WBOY
Release: 2016-07-13 10:46:49
Original
960 people have browsed it

CSV is the English abbreviation of (Comma Separated Values), which are usually plain text files. It is recommended to use WORDPAD or Notepad (NOTE) to open it, and then save it as a new file and then open it with EXCEL. You can also open it directly with excel, just like the excel file.

The reference code for php to generate excel files (csv) is as follows:

 代码如下 复制代码
header("Content-type:application/vnd.ms-excel");
header("Content-Disposition:attachment;filename= www.bKjia.c0m test_data.xls");
//输出内容如下:
echo "姓名"."t";
echo "年龄"."t";
echo "学历"."t";
echo "n";
echo "张三"."t";
echo "25"."t";
echo "本www.111Cn.net科"."t";
?>

The following method is taken from a project (only supports 1997-2003) t is Tab

// Avoid outputting the following content
The code is as follows
 代码如下 复制代码

/**
* 导出 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('dtype');
// 查询数据库内的信息
$datas = $dtype->select();
            // 循环输出每条信息
            foreach($datas as $data){
                $exc .="{$data['name']}t{$data['type']}t{$data['area']}t{$data['capacity']}t{$data['rent']}t{$data['remark']}tn";
            }
            // UTF-8 转换成 GB2312
            $exc =iconv("UTF-8","GB2312",$exc);
            echo $exc;

           // 避免输出下面的内容

            exit();
        }
?>

Copy code

          /**
* Export Excel table
*
                     */
         public function excel(){
// Indicates that the output is an excel file
header("Content-type:application/vnd.ms-excel;");
//The output file name is lamp_dtype.xls
header("Content-Disposition:filename=lamp_dtype.xls");
                    $exc .= "Type name t dormitory category t dormitory area t number of people t monthly rent/person t remarks tn";
               $dtype = D('dtype');
                                               // Query the information in the database
                $datas = $dtype->select();
// Loop through each piece of information
               foreach($datas as $data){
$exc .="{$data['name']}t{$data['type']}t{$data['area']}t{$data['capacity']}t{$data[' rent']}t{$data['remark']}tn";
             }
                     // Convert UTF-8 to GB2312
               $exc =iconv("UTF-8","GB2312",$exc);
                  echo $exc;

exit();
         }
?> ================================================== == Generate csv: The comma is Tab

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632918.htmlTechArticleCSV is the English abbreviation of (Comma Separated Values), which are usually plain text files. It is recommended to use WORDPAD or Notepad (NOTE) to open it, and then save a new file first and then open it with EXCEL, or you can directly...
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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template