PHP code example of wenhainan / thinkcsv

1. Go to this page and download the library: Download wenhainan/thinkcsv library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wenhainan / thinkcsv example snippets


    //引入 
    use think\wenhainan\Thinkcsv;
    
    //浏览器渲染导出csv
    $header = ['姓名', '性别', '手机号'];
    $data = [
        ['小明', '男', 17699019191],
        ['小红', '男', 17699019191],
        ['小黑', '女', 17699019191],
        ['小白', '女', 17699019191],
    ];
    //浏览器访问渲染下载
    $csv = new Thinkcsv('demo.csv',$header,$data);
    $csv->export();
    
    //无需浏览器直接访问的方法执行,比如在调用另外一个方法。本例文件生成在   /网站根目录/upload/demo.csv
    $csv = new Thinkcsv('upload/demo.csv',$header,$data);
    $csv->csvtoFile();
    
    //读取文件 $filepath文件路径
    $filepath = 'public/demo.csv';
    $data = Thinkcsv::readCsvData($filepath);          //默认去除标题行
    $data = Thinkcsv::readCsvData($filepath,false);    //不去除标题行