ネット上にはPHPExcelのファイルを出力というのもはあるのですが、データベースへ入力するというのが見つからず困っています。
挿入時に、データベースにある情報を一度空にして、再度Excelファイル(xlsx拡張子とxls拡張子)の情報を挿入したいと考えています。csvも考えましたが、データの中に,が出てくるためPHPEXCELを使うことにしました。
- 行ったこと。
- composerでインストール
vendor\phpoffice\phpexcel\Classes\PHPExcel.php
vendor\phpoffice\phpexcel\Classes\PHPExcel
がパス。
- データベース情報
MySQL
1create table lists( 2id INT AUTO_INCREMENT primary key, 3item_id VARCHAR(255) NOT NULL, 4name VARCHAR(255) NOT NULL, 5description text NOT NULL, 6price INT NOT NULL, 7);
- controller
PHP
1<?php 2namespace App\Controller; 3use App\Controller\AppController; 4use PHPExcel; 5 6public function insert() 7 { 8 9 $list = $this->Lists->newEntity(); 10 if ($this->request->is('post')) { 11 $readfile = $this->request->data['excelfile']['tmp_name']; 12 $objPHPExcel = PHPExcel_IOFactory::load($readFile); 13 14 } 15 $this->set(compact('list')); 16 }
- view(insert.ctp)
PHP
1 <?php 2 echo $this->Form->create($lists, ['type' => 'file', 'class' => 'lists_form']); 3 echo $this->Form->input('excelfile', ['type' => 'file']); 4 echo $this->Form->button('登録', ['class' => 'submit']); 5 echo $this->Form->end(); 6 ?>
- model(ListsTable.php)
PHP
1 $validator 2 ->requirePresence('name', 'create') 3 ->notEmpty('name');//など
###問題となっていること。
PHPExcelが読み込めない。
をcontorollerやviewに書いても通らない。
Error: Class 'App\Controller\PHPExcel_IOFactory' not found
###追記
Error: Class 'App\Controller\PHPExcel_IOFactory' not found
↓
完全修飾名を付けて対応。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。