php操作excel到数据库

发布于:2020-08-08 17:32:10

<?php
/**
 * Created by PhpStorm.
 * User: Anteater
 * Date: 2018/1/20
 * Time: 11:32
 */
//include_once "./Classes/PHPExcel.php";
 
class CsvReader {
    private $csv_file;
    private $spl_object = null;
    private $error;
 
    public function __construct($csv_file = '') {
        /*判断变量是否存在  &&  文件是否存在*/
        if($csv_file && file_exists($csv_file)) {
            $this->csv_file = $csv_file;
        }
    }
 
    public function set_csv_file($csv_file) {
        if(!$csv_file || !file_exists($csv_file)) {
            $this->error = 'File invalid';
            return false;
        }
        $this->csv_file = $csv_file;
        $this->spl_object = null;
    }
 
    public function get_csv_file() {
        return $this->csv_file;
    }
    /*判断文件是否存在  是否可读*/
    private function _file_valid($file = '') {
        $file = $file ? $file : $this->csv_file;
        if(!$file || !file_exists($file)) {
            return false;
        }
        if(!is_readable($file)) {
            return false;
        }
        return true;
    }
 
    private function _open_file() {
        if(!$this->_file_valid()) {
            $this->error = 'File invalid';
            return false;
        }
        if($this->spl_object == null) {
            $this->spl_object = new SplFileObject($this->csv_file, 'rb');
        }
        return true;
    }
 
    public function get_data($length = 0, $start = 0) {
        if(!$this->_open_file()) {
            return false;
        }
        $length = $length ? $length : $this->get_lines();
        $start = $start - 1;
        $start = ($start < 0) ? 0 : $start;
        $data = array();
        $this->spl_object->seek($start);
        while ($length-- && !$this->spl_object->eof()) {
            $data[] = $this->spl_object->fgetcsv();
            $this->spl_object->next();
        }
        return $data;
    }
 
    public function get_lines() {
        if(!$this->_open_file()) {
            return false;
        }
        $this->spl_object->seek(filesize($this->csv_file));
        return $this->spl_object->key();
    }
 
    public function get_error() {
        return $this->error;
    }
}//获取文件
$csv_file="./demo.csv";
 
$csvreader = new CsvReader($csv_file);
//获得csv文件有多少行
$line_number = $csvreader->get_lines();
//获得csv文件的数据
$data = $csvreader->get_data($line_number);//链接数据库
$link=mysqli_connect('127.0.0.1','root','','husky');
$facedata=[];
foreach ($data as $k=>$da){
        if($k>0){
 
            //产品地址
            $facedata['ids']=$da[0];
            $facedata['title']=$da[6];
            $facedata['description']=$da[7];
            $facedata['ava']="in stock";
            $facedata['condition']="new";
            $facedata['shipping_weight']=$da[9];
            $facedata['price']=$da[14]." USD";
            var_dump($da);
            $string=$da[46];
            $faceda=explode(',',$string);
 
            foreach ($faceda as $k1=>$v1){
                $faceda[$k1]=explode('=',$v1);
 
            }
            $facedata['color']=$faceda[2][1];
            $facedata['google_product_category']=str_replace("/",">",$da[4]);
            $facedata['material']=$faceda[1][1];
            var_dump($faceda);
            $facedata['link']="https://www.onedaywholesale.com/".$da[17].".html";
            $facedata['image_link']="https://www.onedaywholesale.com/".$da[21];
            $facedata['brand']=$faceda[4][1];
 
            $str='';
            foreach ($facedata as $fk=>$face){
                $str .= '`'.$fk.'`' .'='. '"'.$face.'"' .',';
            }
 
            $str=substr($str, 0, -1);
 
//            $des="http://www.onedaywholesale.com/pub/media/catalog/product".$da[21];
            $sql='insert  into faceboolproduct set '.$str;
 
            $res=mysqli_query($link,$sql);
            var_dump($res);
            echo "<br />";
 
        }}


阅读 97+

一片空白

父爱如山,不善表达。回想十多年前,总记得父亲有个宽厚的肩膀,小小的自己跨坐在上面,越过人山人海去看更广阔的天空,那个时候期望自己有一双翅膀,能够像鸟儿一样飞得高,看得远。虽然父亲有时会和自己开玩笑,但在做错事的时候会受到严厉的训斥。父亲有双粗糙的大手掌。