* @version : HUOSDK 8.0 */ namespace huolib\tool; use ZipArchive; class Export { const MAX_ROWS = 10000; /* 一个文件最多1万行 */ public static function ins() { return new static(); } /** * 因为EXCEL单表只能显示104W数据,同时使用PHPEXCEL容易因为数据量太大而导致占用内存过大, * 因此,数据的输出用csv文件的格式输出,但是csv文件用EXCEL软件读取同样会存在只能显示104W的情况, * 所以将数据分割保存在多个csv文件中,并且最后压缩成zip文件提供下载 * * @param array $head 数据头 * @param array $data 数据 * @param string $path 存储的相对路径 * @param string $file_name 文件名称 * @param string $extend 扩展 * @param bool $is_output 是否直接输出 */ public static function exportCsv($head, $data, $path = '', $file_name = "", $extend = '.csv', $is_output = false) { if (empty($path)) { $_root_path = RUNTIME_PATH.'export'; } else { $_root_path = RUNTIME_PATH.'export'.DS.$path; } if (!is_dir($_root_path)) { @mkdir($_root_path, 0755, true); } set_time_limit(0); if (empty($file_name)) { $_file_name = date('YmdHi', time()).$extend; } else { $_file_name = $file_name.$extend; } $_limit = self::MAX_ROWS; $_cnt = 0; $_file_path = $_root_path.DS.$_file_name; $_file_path = iconv('UTF-8', 'GBK', $_file_path); // 逐行取出数据,不浪费内存 $_fp = fopen($_file_path, 'w'); //生成临时文件 // 将数据通过fputcsv写到文件句柄 if (!empty($head)) { foreach ($head as $_key => $_val) { $head[$_key] = iconv('utf-8', 'gbk', $_val); } fputcsv($_fp, $head); } foreach ($data as $_row) { $_cnt++; if ($_limit == $_cnt) { //刷新一下输出buffer,防止由于数据过多造成问题 ob_flush(); flush(); $_cnt = 0; } foreach ($_row as $_key => $_val) { $_row[$_key] = iconv('utf-8', 'gbk//IGNORE', $_val); } fputcsv($_fp, $_row); } fclose($_fp); //每生成一个文件关闭 if ($is_output) { // 输出Excel文件头,可把user.csv换成你要的文件名 header('Content-Type: application/vnd.ms-excel;charset=utf-8'); header('Content-Disposition: attachment;filename="'.$_file_name.'"'); header('Cache-Control: max-age=0'); @readfile($_file_path);//输出文件; exit; } } /** * 输出为压缩文件 * * @param array $file_name_arr 需要压缩的文件名 * @param string $path 存储的相对路径 * @param string $mark 压缩文件名称 */ public static function exportZip($file_name_arr, $path = '', $mark) { if (empty($file_name_arr)) { return; } if (empty($path)) { $_root_path = RUNTIME_PATH.'export'; } else { $_root_path = RUNTIME_PATH.'export'.DS.$path; } if (!is_dir($_root_path)) { @mkdir($_root_path, 0755, true); } //进行多个文件压缩 $_zip_class = new ZipArchive(); $_file_name = $mark.".zip"; $_file_path = $_root_path.DS.$_file_name; $_file_path = iconv('UTF-8', 'GBK', $_file_path); $_zip_class->open($_file_path, ZipArchive::CREATE); //打开压缩包 foreach ($file_name_arr as $file) { $_basename = $file; $file = $_root_path.DS.$file; $file = iconv('UTF-8', 'GBK', $file); $_zip_class->addFile($file, $_basename); //向压缩包中添加文件 } $_zip_class->close(); //关闭压缩包 //输出压缩文件提供下载 header("Cache-Control: max-age=0"); header("Content-Description: File Transfer"); header('Content-disposition: attachment; filename='.$_file_name); // 文件名 header("Content-Type: application/zip"); // zip格式的 header("Content-Transfer-Encoding: binary"); // header('Content-Length: '.filesize($_file_path)); // @readfile($_file_path);//输出文件; @unlink($_file_path); //删除压缩包临时文件 exit; } }