[英]phpexcel and csv save to zip using CI zip library
我想以xlsx和csv格式導出一些數據。 最近我創建了一些功能來做到這一點。 我的想法是:1.將xlsx和csv保存在臨時文件中; 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_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.