【发布时间】:2018-06-30 20:02:03
【问题描述】:
我正在尝试使用 codeigniter 和 phpexcel 将数据从 excel 文件 (.xlsx) 导入到 oracle,这是我的控制器:
private $filename;
public function form(){
$data = array();
if(isset($_POST['preview'])){
$upload = $this->RoadmapModel->upload_file($this->filename);
$upload_data = $this->upload->data();
$this->filename = $upload_data['file_name'];
if($upload['result'] == "success"){
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$this->filename);
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data['sheet'] = $sheet;
}else{ // Jika proses upload gagal
$data['upload_error'] = $upload['error'];
}
}
$this->load->view('form', $data);
}
public function import(){
include APPPATH.'third_party/PHPExcel/PHPExcel.php';
$excelreader = new PHPExcel_Reader_Excel2007();
$loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form());
$sheet = $loadexcel->getActiveSheet()->toArray(null, true, true ,true);
$data = [];
$numrow = 1;
foreach($sheet as $row){
if($numrow > 1){
// Kita push (add) array data ke variabel data
array_push($data, [
'TAHUN'=>$row['A'],
'PROVINCEID'=>$row['B'],
'PROVINSI'=>$row['C'],
'PLAN_DESAB'=>$row['D'],
'ACTUAL_DESAB'=>$row['E'],
'PLAN_ELEKTRIFIKASI'=>$row['F'],
'ACTUAL_ELEKTRIFIKASI'=>$row['G'],
'PLAN_LISDES'=>$row['H'],
'ACTUAL_LISDES'=>$row['I'],
]);
}
$numrow++;
}
$this->RoadmapModel->insert_multiple($data);
redirect("Roadmap");
}
这是我的模型:
public $tablename = "X";
function upload_file($filename){
$this->load->library('upload');
$config['upload_path'] = './excel/';
$config['allowed_types'] = 'xlsx';
$config['max_size'] = '2048';
$config['overwrite'] = true;
$config['file_name'] = $filename;
$this->upload->initialize($config);
if($this->upload->do_upload('file')){
$return = array('result' => 'success', 'file' => $upload_data = $this->upload->data(), 'error' => '');
return $return;
}else{
$return = array('result' => 'failed', 'file' => '', 'error' => $this->upload->display_errors());
return $return;
}
}
function insert_multiple($data){
$p_tablename= $this->tablename;
$this->db->insert_batch($p_tablename, $data);
}
当我使用导入功能时,这是错误消息:
消息:ZipArchive::getFromName():Zip 对象无效或未初始化
文件名:Reader/Excel2007.php
行号:327
回溯:
文件:C:\xampp\htdocs\web_excel_ci\application\controllers\Roadmap.php
行:82
功能:加载
line : 82 is $loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form()); in function import()
用于加载要导入的 excel 文件,我尝试使用 $this->filename = $this->form() 从函数 form() 中获取文件名,但这会出错
请帮我解决问题,因为我已经在那里堆叠了
非常感谢...
【问题讨论】:
标签: php oracle codeigniter phpexcel phpexcelreader