[英]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.