i want to export some data with xlsx and csv format. 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.
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. there is no error, but the excel file doesn't saved at the zip file
Here is my function for creating the excel file
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,
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
Try to save the xls file first, then load it using PHPExcel_IOFactory::load
, then pass the loaded xls file as the zip parameter :
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.