🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
### 导出为excel > $fileName = '表名',$headArr = ['关键词', '出现次数'](对应excel的表头); ```php $data = [ [ '数据1', '数据 2' ], [ '数据1', '数据2' ] ]; ``` ```php /** * 导出词云表 * User: mi * Date: 2020-09-29 */ public function chu_ci_list() { $fileName = '词云图统计表'; $headArr = ['关键词', '出现次数']; $ci_list = Cache::get('ci_list'); $ci_data = unserialize($ci_list); $data = array(); $i = 0; foreach ($ci_data as $k => $item){ $data[$i][0] = $k; $data[$i][1] = $item; $i++; } $fileName .= "-" . date("YmdHi", time()) . ".xlsx"; $objPHPExcel = new \PHPExcel(); $objPHPExcel->getProperties(); $key = ord("A"); // 设置表头 foreach ($headArr as $v) { $colum = chr($key); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v); $objPHPExcel->setActiveSheetIndex(0)->setCellValue($colum . '1', $v); $key += 1; } $column = 2; $objActSheet = $objPHPExcel->getActiveSheet(); foreach ($data as $key => $rows) { // 行写入 $span = ord("A"); foreach ($rows as $keyName => $value) { // 列写入 $objActSheet->setCellValue(chr($span) . $column, $value); $span++; } $column++; } $fileName = iconv("utf-8", "gb2312", $fileName); // 重命名表 $objPHPExcel->setActiveSheetIndex(0); // 设置活动单指数到第一个表,所以Excel打开这是第一个表 header('Content-Type: application/vnd.ms-excel'); header("Content-Disposition: attachment;filename=$fileName"); header('Cache-Control: max-age=0'); $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5'); $objWriter->save('php://output'); // 文件通过浏览器下载 exit(); } ```