首页 文章

codeigniter phpexcel错误ZipArchive :: getFromName():无效或未初始化的Zip对象

提问于
浏览
1

我正在尝试从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函数:加载

line:82是函数import()中的 $loadexcel = $excelreader->load('excel/'.$this->filename = $this -> form());

这是excel文件将导入的加载,我尝试从 $this->filename = $this->form() 函数form()获取文件名,但这是一个错误

请帮忙解决问题我在那里堆叠

非常感谢...

3 回答

  • 1

    From the Docs

    默认情况下,PHPWord使用Zip扩展来处理ZIP压缩存档和其中的文件 . 如果您的服务器上没有安装Zip扩展,则可以使用PHPWord中包含的纯PHP库替代PclZip .

    \PhpOffice\PhpWord\Settings::setZipClass(\PhpOffice\PhpWord\Settings::PCLZIP);
    

    这对我有用 .

  • 0

    在这个堆栈溢出搜索了很多个小时后,我有一个解决方案来解决这个问题

    我使用 $this->session->set_flashdata('fileName',$fileName); 获取一个函数中的值

    并使用 $fileName = $this->session->flashdata('fileName'); 将该值放入另一个函数中

    它的工作原理 .

    感谢您的关注......

  • 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);
    

    但是你必须接受视图也会被加载 .

相关问题