在进行数据导出的时候,少量的数据可以使用phpexcel,但大量的数据用phpexcel就很消耗资源了。
在使用fastadmin做数据导出的时候,相关的代码请参考:https://blog.csdn.net/bingyu709/article/details/141949034
我自己这边因为数据量会很大,所以代码层做了一个数量的划分,少于50000走phpexcel导出,大于50000走csv,并且打包zip。
并且我的逻辑是导出成功后把文件防止项目指定目录下,并没有做一个直接下来,如果想直接从浏览器下载请自己网上找其他代码参考,我所参考的帖子是直接下载的,大家可以试试看。
public function export() {if ($this->request->isPost()) {$filter = $this->request->request("filter", '');$filter_arr = (array)json_decode($filter, true);$ids = $this->request->request('ids','');$scene_id = CService::MANAGE_ALL_CUSTOMER;if (!empty($filter_arr) && isset($filter_arr['scene_id'])) {$scene_id = intval($filter_arr['scene_id']);}$scene_where = $this->getFilterAllSceneWhere($scene_id);list($where, $sort, $order, $offset, $limit) = $this->buildAllParams();if (!empty($ids) && $ids != '' && $ids != 'all') {$ids_arr = explode(',',$ids);$where['id'] = ['in', $ids_arr];}$where = array_merge($scene_where,$where);$count = CModel::model()->where($where)->count();if ($count > 0) {if ($count > 50000) {set_time_limit(0);ini_set('memory_limit', '128M');$xlsTitle = [ '字段1', '字段2', '字段3'];// buffer计数器$cnt = 0;$fileName = '客户导出'.date('YmdHis').rand(10000000,88888888);//文件名称$pro_path = ExportService::EXPORT_CUSTOMER_FILE_PATH; // 项目目录$fileName = $pro_path . $fileName;$zip_file_name = $fileName.'.zip';// 输出Excel文件头,可把user.csv换成你要的文件名header('Content-Type: application/vnd.ms-excel;charset=utf-8');header('Content-Disposition: attachment;filename="' . $zip_file_name . '"');header('Cache-Control: max-age=0');$fileNameArr = [];$every_step_limit = 10000;$loop_number_count = intval(ceil($count / $every_step_limit));for ($i = 0; $i < $loop_number_count; $i ++) {$fp = fopen($fileName . '_' . ($i+1) . '.csv', 'w'); //生成临时文件fwrite($fp, chr(0xEF).chr(0xBB).chr(0xBF));/*防止乱码 */// chmod('attack_ip_info_' . $i . '.csv',777);//修改可执行权限$fileNameArr[] = $fileName . '_' . ($i+1) . '.csv'; // 将数据通过fputcsv写到文件句柄 fputcsv($fp, $xlsTitle);/******************** 调整位置开始 ***************************/$limit = $i * $every_step_limit;$limit_list = ExportService::service()->getExportList($where, $limit, $every_step_limit);$list = ExportService::service()->formatExportData($limit_list, $this->auth_global_search_organise_id);if (!empty($list)) {foreach ($list as $item) {$cnt++;if ($limit == $cnt) {// 刷新一下输出buffer,防止由于数据过多造成问题 ob_flush();flush();$cnt = 0;}fputcsv($fp, $item);}unset($list);}fclose($fp); // 每生成一个文件关闭 }try {// 进行多个文件压缩$zip = new \ZipArchive();$zip->open($zip_file_name, $zip::CREATE); // 打开压缩包foreach ($fileNameArr as $file) {$zip->addFile($file, basename($file)); // 向压缩包中添加文件 }$zip->close(); // 关闭压缩包foreach ($fileNameArr as $file) {unlink($file); // 删除csv临时文件 }if (file_exists($zip_file_name)) {$this->success('操作成功');} else {$this->error('操作失败');}} catch (ValidateException|PDOException|Exception $e) {$this->error('操作失败');}} else {set_time_limit(0);$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();$sheet->getStyle('A:S')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER) //设置垂直居中->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER) //设置水平居中->setWrapText(true); //设置自动换行 $titleStyleArray = ['font' => ['name' => 'SimHei', // 中文字体,"黑体"的类似字体'bold' => true,'size' => 12,]];$sheet->getStyle('A1:Z1')->applyFromArray($titleStyleArray);$lineStyleArray = ['font' => ['name' => 'SimHei', // 中文字体,"黑体"的类似字体 ]];//设置表头$sheet->setCellValue('A1', '字段1');$sheet->setCellValue('B1', '字段2');$sheet->setCellValue('C1', '字段3');
.......$sheet->getColumnDimension('A')->setWidth(15);$sheet->getColumnDimension('B')->setWidth(15);$sheet->getColumnDimension('C')->setWidth(15);
.......//$sheet->getDefaultRowDimension()->setRowHeight(60); //设置行高 $res_list = ExportService::service()->getExportCustomerList($where);$list = ExportService::service()->formatExportCustomerData($res_list, $this->auth_global_search_organise_id);if (!empty($list)) {//输出表格foreach($list as $keys => $value) {$keys = $keys + 2;$sheet->setCellValue('A'.$keys,$value['field1'] ?? '')->getStyle('A')->applyFromArray($lineStyleArray);$sheet->setCellValue('B'.$keys,$value['field2'] ?? '')->getStyle('B')->applyFromArray($lineStyleArray);$sheet->setCellValue('C'.$keys,$value['field3'] ?? '')->getStyle('C')->applyFromArray($lineStyleArray);
.........
}}// 保存Excel文件到服务器的指定路径$pro_path = ExportService::EXPORT_CUSTOMER_FILE_PATH; // 项目目录$file_name = '导出'.date('YmdHis').rand(10000000,88888888).'.xlsx';$file_path = $pro_path . $file_name;// if (!file_exists($file_path)) { // fopen($file_path, 'w'); // }try {$writer = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheet, 'Xlsx');$writer->save($file_path);if (file_exists($file_path)) {$this->success('操作成功');} else {$this->error('操作失败');}} catch (ValidateException|PDOException|Exception $e) {$this->error('操作失败');}}}}}