我正在尝试从excel文件(.xlsx)导入数据到oracle使用codeigniter和phpexcel,这是我的控制器:
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
Backtrace:
文件:C:\ xampp \ htdocs \ web_excel_ci \ application \ controllers \ Roadmap.php
线:82
功能:加载
行:82是函数import()
中的$loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form());
对于excel文件将要导入的加载,我尝试使用$this->filename = $this->form()
从函数form()获取文件名,但这是一个错误
请帮助解决问题,我已经堆积在那里
非常感谢...
答案 0 :(得分:1)
<强> From the Docs 强>
默认情况下,PHPWord使用Zip扩展来处理ZIP压缩存档及其中的文件。如果您的服务器上没有安装Zip扩展,则可以使用PHPWord中包含的纯PHP库替代PclZip。
\PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
这对我有用。
答案 1 :(得分:0)
线路确实存在问题
$loadexcel = $excelreader->load('excel/'.$this->filename = $this->form());
似乎是尝试使用$this->filename
的回复设置$this->form()
。但是$this->form()
没有返回值 - 它会加载视图。
因此,传递给$excelreader->load()
的参数可能只是字符串“excel /”。确定这不是一个有效的zip对象。
此序列应为excelreader->load()
生成正确的字符串。
$this->form();
$loadexcel = $excelreader->load('excel/'.$this->filename);
但你必须接受视图也会被加载。
答案 2 :(得分:0)
我在这个堆栈溢出搜索了很多个小时后得到了解决此案例的解决方案,
我使用$this->session->set_flashdata('fileName',$fileName);
获取一个函数中的值
并使用$fileName = $this->session->flashdata('fileName');
将该值放入另一个函数
它已经奏效了。
感谢您的关注......