简体   繁体   English

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

[英]phpexcel and csv save to zip using CI zip library

i want to export some data with xlsx and csv format. 我想以xlsx和csv格式导出一些数据。 recently i create some function to do it. 最近我创建了一些功能来做到这一点。 and my idea is : 1.save the xlsx and the csv in the temp, 2. add it to the zip library 3. download it. 我的想法是:1.将xlsx和csv保存在临时文件中; 2.将其添加到zip库中; 3.下载它。

i have tried to make some functions, and the csv is show an output in the zip, but the excel is not show any output. 我试图做一些功能,而CSV在zip中显示输出,但是excel不显示任何输出。 there is no error, but the excel file doesn't saved at the zip file 没有错误,但是excel文件没有保存在zip文件中

Here is my function for creating the excel file 这是我创建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'));

    }

and here is the function to create the csv file, 这是创建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);
    }

and here the code to asssembled the function and download it at the same time. 此处是用于组装该功能并同时下载该功能的代码。

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

what i expect is to download the csv and the excel at the same time 我期望的是同时下载csv和excel

Try to save the xls file first, then load it using PHPExcel_IOFactory::load , then pass the loaded xls file as the zip parameter : 尝试先保存xls文件,然后使用PHPExcel_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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM