先看图
一、介绍
xlswriter是一个高效处理excel文件的PHP扩展,底层以C语言实现;处理速度是PHPExcel几十倍甚至几百倍的效率。
官方链接:https://gitee.com/viest/php-ext-xlswriter
缺点:更深入的功能(例如读取excel图片)健全;导出excel样式不够丰富
导出excel文件,xlswriter绝对是效率最高的处理方式,配合php后台任务导出百万千万级别数据也没有问题
通用导出类:
class ExportOperateAnaly { public $export_limit = 13000; /** * 统一导出 * @Author zhibin3 * @DateTime 2022-12-24 * @param array $pageData [description] * @return [type] [description] */ public function layout(array $params, $is_export=true) { ini_set('memory_limit', '512M'); $header_list = ['商品ID', '商品标题', '商品图片']; $width_list = ['商品图片'=>55,'商品标题'=>55]; return $this->export($header_list, $list, '商品运营分析', $is_export, $width_list); } /** * 通用导出方法 * @Author zhibin3 * @DateTime 2023-02-14 * @param array $header_list [description] * @param array $list [description] * @return [type] [description] */ public function export(array $header_list, array $datas, $filename='选款导出列表', $is_export=true, array $width_list=[]) { $config = [ 'path' => ROOT_PATH . 'public' . DS . 'dl' // xlsx文件保存路径 ]; $Excel = new \Vtiful\Kernel\Excel($config); $filename .= datetime(time(), 'Y-m-d_H_i_s').'.xlsx'; $fileObject = $Excel->fileName($filename, 'sheet1')->header($header_list); $fileHandle = $fileObject->getHandle(); $format = new \Vtiful\Kernel\Format($fileHandle); $boldStyle = $format->bold()->toResource(); $alignStyle = $format->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource(); // $backgroundStyle = $format->background(0xD9D9D9)->toResource(); foreach ($datas as $index => $val) { $column = 0; foreach ($val as $key => $value) { $row = $index+1; /*if ($key=='img') { $return = ImageImport::export_image_save($value); $img_path = $return['image'] ? $return['path'] : $value; // pre($value, $return['image'], $row, $column, $img_path); $fileObject->insertImage($row, $column, $img_path, 0.1, 0.1); }*/ if ($key=='lowest_price') { $fileObject->insertFormula($row, $column, $value); } else if (is_string($value)) { $fileObject->insertText($row, $column, $value); } else { $fileObject->insertText($row, $column, $value); } $column++; } } // pre($datas);die; foreach ($header_list as $key => $value) { // $letter = $header_map[$key]; $letter = \Vtiful\Kernel\Excel::stringFromColumnIndex($key); $width_value = $width_list[$value] ?? 20; $fileObject->setRow($letter.($key+1), 35, $boldStyle); $fileObject->setColumn($letter.':'.$letter, $width_value, $alignStyle); } $format2 = new \Vtiful\Kernel\Format($fileHandle); $alignStyle = $format2->align(\Vtiful\Kernel\Format::FORMAT_ALIGN_CENTER, \Vtiful\Kernel\Format::FORMAT_ALIGN_VERTICAL_CENTER)->toResource(); $wrapStyle = $format2->wrap()->toResource(); foreach ($datas as $index => $value) { $row = $index+2; $fileObject->setRow('A'.$row, 30, $alignStyle); } // pre($fileObject);die; $filePath = $fileObject->output(); if ($is_export) { header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); header('Content-Disposition: attachment;filename="' . $filename . '"'); header('Content-Length: ' . filesize($filePath)); header('Content-Transfer-Encoding: binary'); header('Cache-Control: must-revalidate'); header('Cache-Control: max-age=0'); header('Pragma: public'); if (ob_get_contents()) ob_clean(); flush(); if (@copy($filePath, 'php://output') === false) throw new \Exception($filePath. '地址出问题了'); @unlink($filePath); return ['code'=>1, 'msg'=>'导出成功', 'file_path'=>'']; } else { return ['code'=>1, 'msg'=>'导出成功', 'file_path'=>'dl/'.$filename]; } } }
二、golang
标签:return,Excel,list,value,xlswriter,golang,header,导出,fileObject From: https://www.cnblogs.com/cnxzb/p/18005886