数据导出,简单的csv导出,
public static function export($parameter){ if (is_array($parameter)) { $filename = date(‘Y-m-d_H-i-s‘) . ‘.csv‘; header(‘Content-Type: text/csv‘); header("Content-Disposition: attachment;filename={$filename}"); $fp = fopen(‘php://output‘, ‘w‘); fwrite($fp,chr(0xEF).chr(0xBB).chr(0xBF)); if ( ! empty($parameter[‘header‘]) && is_array($parameter[‘header‘])) { fputcsv($fp, $parameter[‘header‘]); } if (isset($parameter[‘data‘])) { foreach ($parameter[‘data‘] as $row) { fputcsv($fp, $row); } }return true; } throw new \yii\web\HttpException(500, "Not a valid parameter!"); }
$parameter[‘header‘]-------表头
$parameter[‘data‘]-------数据 另外的资料:http://www.yiichina.com/extension/464?sort=desc
时间: 2024-10-22 14:08:58