123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475 |
- <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
- class Test extends CI_Controller {
-
- var $response;
- var $userinfo;
- var $version;
- public function __construct() {
- parent::__construct();
- $this->load->model('User_model');
- $this->response = $this->config->config['response_en'];
- $version = 1;
- $this->session->set_userdata('version', $version);
- $this->version = $version;
- }
- // 批量导入
- public function haha(){
- $indexArr = ['B'=>'b','C'=>'c'];
- // 文件上传成功
- // $data = $this->upload->data();
- $filePath = '../upload/test.xlsx';
- $this->load->library('phpExcel/PHPExcel');
- $extension = strtolower( pathinfo($filePath, PATHINFO_EXTENSION) );
- if($extension =='xls' ){
- $reader = PHPExcel_IOFactory::createReader('Excel5');
- }elseif($extension =='xlsx'){
- $reader = PHPExcel_IOFactory::createReader('Excel2007');
- }else if ($extension=='csv' || strtolower($extension)=='hcl') {
- $reader = new PHPExcel_Reader_CSV();
- }
- // $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
- $PHPExcel = $reader->load($filePath); // 载入excel文件
- $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
- $highestRow = $sheet->getHighestRow(); // 取得总行数
- // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
-
- /** 循环读取每个单元格的数据 */
- $k = 0;
- $data = array();
- for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
- {
- if ($row < 4) continue; // 跳过标题行
- foreach ($indexArr as $key => $value) {
- $res = $sheet->getCell($key.$row)->getValue();
- if (is_object($res)) {
- $res= $res->__toString();
- }
- $data[$k][$value] = $res;
- }
- $k ++;
- }
- $supId = 0;
- foreach ($data as $key => $value) {
- if (isset($value['b']) && !empty($value['b'])) {
- $res = $this->db->query('select * from company where name = "'.$value['b'].'"')->row_array();
- // if (empty($res)) var_dump($value['b']);
- $supId = $res['id'];
- // var_dump($res);
- }
- if (!empty($value['c'])) $this->db->query('insert into model_info (supId,name) values('.$supId.',"'.$value['c'].'")');
- }
- var_dump($data);die;
- // 批量写入数据
- return $data;
- }
- }
|