php操作excel到数据库
<?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 />"; }}
相关推荐
-
php检测数据规则 php
2019-1-7
-
导出数据到表格PHP php
2019-1-7
-
php 通用redis类 php
2019-1-8
-
PHP生成自定义验证码 php
2019-1-8
-
PHP加密解密函数authcode的用法详细解析 php
2019-1-7
-
PHP异步 php
2019-1-7
-
http请求简单封装 php
2019-1-7
-
PHPExcel集成对数据导入和导出 php
2019-1-7
-
PHP无限分类 php
2019-1-13
-
php 数组助手类 php
2019-1-7