infoManagementDb = new Info(); } public function getInfos( &$cnt = 0, $orderColumn, $orderDir, $start, $length, $searchValue, $sDateStart, $sDateFinal ) { $infos = $this->infoManagementDb ->select([ 'id', \DB::raw(" (CASE WHEN image_uri <> '' THEN CONCAT( '', '', '' ) ELSE 'X' END) as image_uri "), 'nickname', 'memo', 'hash', 'name', 'tel', 'email', 'zone', 'cdate', ]); // 過濾搜尋條件 if ($sDateStart != '') $infos = $infos->where('cdate', '>=', $sDateStart . ' 00:00:00'); if ($sDateFinal != '') $infos = $infos->where('cdate', '<=', $sDateFinal . ' 23:59:59'); // 取總筆數 $cnt = $infos->count(); // 排序 $infos = $infos ->orderByRaw((int)$orderColumn . ' ' . $orderDir); // 彙整 // 分頁 $infos = $infos ->skip($start)->take($length); // 實際取資料 $infos = $infos ->get() ->toArray(); // 整理返回值並返回 return $infos; } public function getExportInfo($param) { $infos = $this->infoManagementDb ->select([ 'id', \DB::raw(" (CASE WHEN image_uri <> '' THEN CONCAT( '" . env('AWS_S3_URI') . "', image_uri ) ELSE 'X' END) as image_uri "), 'nickname', 'memo', 'hash', 'name', 'tel', 'email', 'zone', 'utm_source', 'utm_medium', 'utm_campaign', 'cdate', 'ip', ]); // 過濾搜尋條件 if ($param["sDateStart"] != '') $infos = $infos->where('cdate', '>=', $param["sDateStart"] . ' 00:00:00'); if ($param["sDateFinal"] != '') $infos = $infos->where('cdate', '<=', $param["sDateFinal"] . ' 23:59:59'); // 實際取資料 $infos = $infos ->get() ->toArray(); // 整理返回值並返回 return $infos; } public function downloadExcel($titles = [], $datas = [], $fileName = 'simple') { $spreadsheet = new Spreadsheet(); ini_set('memory_limit', '1024M'); ini_set("max_execution_time", "600"); $spreadsheet->getActiveSheet() ->fromArray( $titles, // The data to set null, // Array values with this value will not be set 'A1' // Top left coordinate of the worksheet range where we want to set these values (default is A1) ); $spreadsheet->getActiveSheet() ->fromArray( $datas, // The data to set null, // Array values with this value will not be set 'A2' // Top left coordinate of the worksheet range where we want to set these values (default is A1) ); // Redirect output to a client’s web browser (Xlsx) header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="' . $fileName . '.xlsx"'); header('Cache-Control: max-age=0'); // If you're serving to IE 9, then the following may be needed header('Cache-Control: max-age=1'); // If you're serving to IE over SSL, then the following may be needed header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified header('Cache-Control: cache, must-revalidate'); // HTTP/1.1 header('Pragma: public'); // HTTP/1.0 $writer = IOFactory::createWriter($spreadsheet, 'Xlsx'); $writer->save('php://output'); exit; } }