Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用CI zip库将phpexcel和csv保存到zip_Php_Excel_Codeigniter_Csv_Phpexcel - Fatal编程技术网

使用CI zip库将phpexcel和csv保存到zip

使用CI zip库将phpexcel和csv保存到zip,php,excel,codeigniter,csv,phpexcel,Php,Excel,Codeigniter,Csv,Phpexcel,我想导出一些xlsx和csv格式的数据。最近我创建了一些函数来实现它。我的想法是: 1.将xlsx和csv保存在temp中, 2.将其添加到zip库中 3.下载它 我尝试过制作一些函数,csv在zip中显示一个输出,但excel不显示任何输出。没有错误,但excel文件未保存在zip文件中 这是我创建excel文件的函数 public function createExcel($id){ $excel = PHPExcel_IOFactory::load('C:\xampp\ht

我想导出一些xlsx和csv格式的数据。最近我创建了一些函数来实现它。我的想法是: 1.将xlsx和csv保存在temp中, 2.将其添加到zip库中 3.下载它

我尝试过制作一些函数,csv在zip中显示一个输出,但excel不显示任何输出。没有错误,但excel文件未保存在zip文件中

这是我创建excel文件的函数

public function createExcel($id){
        $excel = PHPExcel_IOFactory::load('C:\xampp\htdocs\api-priadi\ContohExcel.xlsx');
        $excel->setActiveSheetIndex(0);

        $resultExcel = $this->AdminM->getDataExcel($id);
        $rows = 3;

        foreach ($resultExcel->result() as $rowExcel){
            $excel->getActiveSheet()->setCellValueByColumnAndRow(0,$rows,$rowExcel->id);
            $excel->getActiveSheet()->setCellValueByColumnAndRow(1,$rows,$rowExcel->name);
            $rows++;
        }

        $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
        $this->zip->add_data('excel'.date('').date('Y_M_d_H_i_s').'.xlsx',$objWriter->save('assets/Haha.xlsx'));

    }
这是创建csv文件的函数

public function createCSV($id){
        $csv = fopen("php://temp", "rw");

        fputcsv($csv, array('Name', 'Email', 'Birthdate'));

        $dataCSV = $this->AdminM->getDataCSV($id);
        foreach ($dataCSV as $row){
            fputcsv($csv, $row);
        }

        global $fileCSV;
        $fileCSV = stream_get_contents($csv,-1,0);

        fclose($csv);

        $this->zip->add_data('csv'.date('').date('Y_M_d_H_i_s').'.csv',$fileCSV);
    }
这里的代码是组装函数并同时下载的

public function download(){
        $id = $this->input->post('id');
        $this->createCSV($id);
        $this->createExcel($id);
        $this->zip->download('testing.zip');
    }

我希望同时下载csv和excel

首先尝试保存xls文件,然后使用
PHPExcel\u IOFactory::load加载它,然后将加载的xls文件作为zip参数传递:

public function createExcel($id){
    $excel = PHPExcel_IOFactory::load('C:\xampp\htdocs\api-priadi\ContohExcel.xlsx');
    $excel->setActiveSheetIndex(0);

    $resultExcel = $this->AdminM->getDataExcel($id);
    $rows = 3;

    foreach ($resultExcel->result() as $rowExcel){
        $excel->getActiveSheet()->setCellValueByColumnAndRow(0,$rows,$rowExcel->id);
        $excel->getActiveSheet()->setCellValueByColumnAndRow(1,$rows,$rowExcel->name);
        $rows++;
    }

    $xls_name = 'assets/Haha.xlsx';
    $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $objWriter->save($xls_name);
    $objPHPExcel = PHPExcel_IOFactory::load($xls_name);
    $this->zip->add_data('excel'.date('').date('Y_M_d_H_i_s').'.xlsx', $objPHPExcel);
}

首先尝试保存xls文件,然后使用
PHPExcel\u IOFactory::load加载它,然后将加载的xls文件作为zip参数传递:

public function createExcel($id){
    $excel = PHPExcel_IOFactory::load('C:\xampp\htdocs\api-priadi\ContohExcel.xlsx');
    $excel->setActiveSheetIndex(0);

    $resultExcel = $this->AdminM->getDataExcel($id);
    $rows = 3;

    foreach ($resultExcel->result() as $rowExcel){
        $excel->getActiveSheet()->setCellValueByColumnAndRow(0,$rows,$rowExcel->id);
        $excel->getActiveSheet()->setCellValueByColumnAndRow(1,$rows,$rowExcel->name);
        $rows++;
    }

    $xls_name = 'assets/Haha.xlsx';
    $objWriter = PHPExcel_IOFactory::createWriter($excel, 'Excel2007');
    $objWriter->save($xls_name);
    $objPHPExcel = PHPExcel_IOFactory::load($xls_name);
    $this->zip->add_data('excel'.date('').date('Y_M_d_H_i_s').'.xlsx', $objPHPExcel);
}

您是否在未压缩文件的情况下测试了
xls
?是的,我已经测试过了。xls工作正常,但是当我试着把xlsx放到zip时,我没有进入zipHave你测试了
xls
,没有压缩文件?是的,我已经测试过了。xls工作得很好,但是当我试着把xlsx放到拉链上时,我并没有进入拉链