PHP----CSV文件处理类

CVS文件的导入和导出php类




class csv
{
    public $csv_array; //csv数组数据
    public $csv_str;  //csv文件数据

    public function __construct($param_arr, $column)
    {
        $this->csv_array = $param_arr;
        $this->path = $path;
        $this->column = $column;
    }

    /**
     * 导出
     * */
    public function export()
    {
        if (empty($this->csv_array) || empty($this->column)) {
            return false;
        }
        $param_arr = $this->csv_array;
        unset($this->csv_array);
        $export_str = implode(',', $param_arr['nav']) . "\n";
        unset($param_arr['nav']);
        //组装数据
        foreach ($param_arr as $k => $v) {
            foreach ($v as $k1 => $v1) {
                $export_str .= implode(',', $v1) . "\n";
            }
        }
        //将$export_str导出
        header("Cache-Control: public");
        header("Pragma: public");
        header("Content-type:application/vnd.ms-excel");
        header("Content-Disposition:attachment;filename=txxx.csv");
        header('Content-Type:APPLICATION/OCTET-STREAM');
        ob_start();
        //  $file_str=  iconv("utf-8",'gbk',$export_str);
        ob_end_clean();
        echo $export_str;
    }

    /**
     * 导入
     * */
    public function import($path, $column = 3)
    {
        $flag = flase;
        $code = 0;
        $msg = '未处理';
        $filesize = 1; //1MB
        $maxsize = $filesize * 1024 * 1024;
        $max_column = 1000;

        //检测文件是否存在
        if ($flag === flase) {
            if (!file_exists($path)) {
                $msg = '文件不存在';
                $flag = true;
            }
        }
        //检测文件格式
        if ($flag === flase) {
            $ext = preg_replace("/.*.([^.]+)/", "$1", $path);
            if ($ext != 'csv') {
                $msg = '只能导入CSV格式文件';
                $flag = true;
            }
        }

        //检测文件大小
        if ($flag === flase) {
            if (filesize($path) > $maxsize) {
                $msg = '导入的文件不得超过' . $maxsize . 'B文件';
                $flag = true;
            }
        }

        //读取文件
        if ($flag == flase) {
            $row = 0;
            $handle = fopen($path, 'r');
            $dataArray = array();
            while ($data = fgetcsv($handle, $max_column, ",")) {
                $num = count($data);
                if ($num < $column) {
                    $msg = '文件不符合规格真实有:' . $num . '列数据';
                    $flag = true;
                    break;
                }

                if ($flag === flase) {
                    for ($i = 0; $i < 3; $i++) {
                        if ($row == 0) {
                            break;
                        }
                        //组建数据
                        $dataArray[$row][$i] = $data[$i];
                    }
                }
                $row++;
            }
        }

        return $dataArray;
    }
}


$param_arr = array(
    'nav' => array('用户名', '密码', '邮箱'),
    array(0 => array('aaa', '123456', 'aaa@yahoo.com'),
        1 => array('bbb', '213456', 'bbb@yahoo.com'),
        2 => array('ccc', '123456', 'ccc@yahoo.com')
    ));

$column = 3;
$csv = new Csv($param_arr, $column);
$csv->export(); //下载

$path = 'd:/tmp.csv';
$import_arr = $csv->import($path, 3);


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值