Home > Backend Development > PHP Tutorial > Implementation code for php to export Excel files in csv format

Implementation code for php to export Excel files in csv format

不言
Release: 2023-04-03 20:02:01
Original
2827 people have browsed it

The content of this article is about the implementation code of exporting Excel files in csv format through PHP. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Rendering

Implementation code for php to export Excel files in csv format

Source code analysis

index.php

<?php
require_once "./Export.php";//测试数据
$headerList= [&#39;列名1&#39;,&#39;列名2&#39;,&#39;列名3&#39;];
$data = [
    [&#39;值1&#39;,&#39;值2&#39;,&#39;值3&#39;],
    [&#39;值11&#39;,&#39;值22&#39;,&#39;值33&#39;],
    [&#39;值111&#39;,&#39;值222&#39;,&#39;值333&#39;]
];
$fileName = "测试导出文件名";
$tmp = [&#39;备份字段1&#39;,&#39;备份值1&#39;,&#39;&#39;,&#39;备份字段2&#39;,&#39;备份值2&#39;];
$export = new Export();$result = $export->exportToCsv($headerList,$data,$fileName,$tmp);
Copy after login

Export.php

<?php
class export{
    /**
     * params $headerList 头部列表信息(一维数组) 必传
     * params $data 导出的数据(二维数组)    必传
     * params $filename 文件名称转码 必传
     * params $tmp 备用信息(二维数组) 选传
     * PS:出现数字格式化情况,可添加看不见的符号,使其正常,如:"\t"
     **/
    public function exportToCsv($headerList = [] , $data = [] , $fileName = &#39;&#39; , $tmp = []){
        //文件名称转码
        $fileName = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $fileName);        //设置header头
        header(&#39;Content-Type: application/vnd.ms-excel&#39;);
        header(&#39;Content-Disposition: attachment;filename=&#39; . $fileName . &#39;.csv&#39;);
        header(&#39;Cache-Control: max-age=0&#39;);        //打开PHP文件句柄,php://output,表示直接输出到浏览器
        $fp = fopen("php://output","a");        //备用信息
        foreach ($tmp as $key => $value) {            
        $tmp[$key] = iconv("UTF-8", &#39;GBK&#39;, $value);
        }       
         //使用fputcsv将数据写入文件句柄
        fputcsv($fp, $tmp);        
        //输出Excel列表名称信息
        foreach ($headerList as $key => $value) {            
        $headerList[$key] = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $value);//CSV的EXCEL支持BGK编码,一定要转换,否则乱码
        }        
        //使用fputcsv将数据写入文件句柄
        fputcsv($fp, $headerList);        //计数器
        $num = 0;        
        //每隔$limit行,刷新一下输出buffer,不要太大亦不要太小
        $limit = 100000;       
         //逐行去除数据,不浪费内存
        $count = count($data);        
        for($i = 0 ; $i < $count ; $i++){           
        $num++;            、
        //刷新一下输出buffer,防止由于数据过多造成问题
            if($limit == $num){
                ob_flush();
                flush();                
                $num = 0;
            }            
            $row = $data[$i];            
            foreach ($row as $key => $value) {                
            $row[$key] = iconv(&#39;UTF-8&#39;, &#39;GBK&#39;, $value);
            }
            fputcsv($fp, $row);
        }
    }
}
Copy after login

Link: https://pan.baidu.com/s/1e9BK6l5fY4aDDgYS7CLUig Password: v120

Related recommendations:

PHP How to implement fuzzy query (image and text code)

How to implement real-time editing of tables with php and ajax (code attached)

The above is the detailed content of Implementation code for php to export Excel files in csv format. For more information, please follow other related articles on the PHP Chinese website!

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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template