Test.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  2. class Test extends CI_Controller {
  3. var $response;
  4. var $userinfo;
  5. var $version;
  6. public function __construct() {
  7. parent::__construct();
  8. $this->load->model('User_model');
  9. $this->response = $this->config->config['response_en'];
  10. $version = 1;
  11. $this->session->set_userdata('version', $version);
  12. $this->version = $version;
  13. }
  14. // 批量导入
  15. public function haha(){
  16. $indexArr = ['B'=>'b','C'=>'c'];
  17. // 文件上传成功
  18. // $data = $this->upload->data();
  19. $filePath = '../upload/test.xlsx';
  20. $this->load->library('phpExcel/PHPExcel');
  21. $extension = strtolower( pathinfo($filePath, PATHINFO_EXTENSION) );
  22. if($extension =='xls' ){
  23. $reader = PHPExcel_IOFactory::createReader('Excel5');
  24. }elseif($extension =='xlsx'){
  25. $reader = PHPExcel_IOFactory::createReader('Excel2007');
  26. }else if ($extension=='csv' || strtolower($extension)=='hcl') {
  27. $reader = new PHPExcel_Reader_CSV();
  28. }
  29. // $reader = PHPExcel_IOFactory::createReader('Excel5'); //设置以Excel5格式(Excel97-2003工作簿)
  30. $PHPExcel = $reader->load($filePath); // 载入excel文件
  31. $sheet = $PHPExcel->getSheet(0); // 读取第一個工作表
  32. $highestRow = $sheet->getHighestRow(); // 取得总行数
  33. // $highestColumm = $sheet->getHighestColumn(); // 取得总列数
  34. /** 循环读取每个单元格的数据 */
  35. $k = 0;
  36. $data = array();
  37. for ($row = 1; $row <= $highestRow; $row++) //行号从1开始
  38. {
  39. if ($row < 4) continue; // 跳过标题行
  40. foreach ($indexArr as $key => $value) {
  41. $res = $sheet->getCell($key.$row)->getValue();
  42. if (is_object($res)) {
  43. $res= $res->__toString();
  44. }
  45. $data[$k][$value] = $res;
  46. }
  47. $k ++;
  48. }
  49. $supId = 0;
  50. foreach ($data as $key => $value) {
  51. if (isset($value['b']) && !empty($value['b'])) {
  52. $res = $this->db->query('select * from company where name = "'.$value['b'].'"')->row_array();
  53. // if (empty($res)) var_dump($value['b']);
  54. $supId = $res['id'];
  55. // var_dump($res);
  56. }
  57. if (!empty($value['c'])) $this->db->query('insert into model_info (supId,name) values('.$supId.',"'.$value['c'].'")');
  58. }
  59. var_dump($data);die;
  60. // 批量写入数据
  61. return $data;
  62. }
  63. }