Advertisement
kura2yamato

controller basic

Aug 6th, 2023 (edited)
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | Source Code | 0 0
  1. <?php
  2. namespace App\Http\Controllers;
  3.  
  4. use Illuminate\Http\Request;
  5. use App\Http\Controllers\Controller;
  6.  
  7. //excel only
  8. use PhpOffice\PhpSpreadsheet\IOFactory;
  9.  
  10. class ExcelController extends Controller
  11. {
  12.     function index(Request $request){
  13.         //dd($request->all());
  14.         $data = [];
  15.         $aFile=[ ];
  16.         foreach(glob(resource_path('excel'). DIRECTORY_SEPARATOR . "*.*") as $file){
  17.             $aFile[]=$file;
  18.         }
  19.         $data['files'] = $aFile;
  20.  
  21.         return view('demo.index', $data);
  22.     }
  23.    
  24.     function ex1(){
  25.         $reader = IOFactory::createReader('Xlsx');
  26.         //template
  27. /*
  28. buat folder excel di resources
  29. masukkan file demo.xlsx kedalamnya
  30. */
  31.         $template=resource_path('excel'). DIRECTORY_SEPARATOR .'demo.xlsx';
  32.  
  33.         $spreadsheet = $reader->load( $template);
  34. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  35. header('Content-Disposition: attachment;filename="001template.xlsx"');
  36. header('Cache-Control: max-age=0');
  37. // If you're serving to IE 9, then the following may be needed
  38. header('Cache-Control: max-age=1');
  39.  
  40. // If you're serving to IE over SSL, then the following may be needed
  41. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  42. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  43. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  44. header('Pragma: public'); // HTTP/1.0
  45.  
  46.         $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  47.         $writer->save('php://output');
  48.  
  49.     }
  50.  
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement