JezK
Edit File: export_warehouse.php
<?php include_once("./app/adminObj.php"); include_once("./app/checkSession.php"); require_once("../vendor/autoload.php"); use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\IOFactory; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header('Content-Disposition: attachment;filename="warehouses.xlsx"'); header('Cache-Control: max-age=0'); header('Cache-Control: max-age=1'); header('Cache-Control: cache, must-revalidate'); header('Pragma: public'); $spreadsheet = new Spreadsheet(); //Specify the properties for this document //To apply auto size to all fields from B to Z foreach(range('B','K') as $columnID) { $spreadsheet->getActiveSheet()->getColumnDimension($columnID) ->setAutoSize(true); } $company_id=$_SESSION['company_id']; $type = isset($_REQUEST['type'])?$_REQUEST['type']:1; $condition='company_id = '.$company_id.' and warehouse_status='.$type; //active if($type==2) // both block and rejected { $condition='company_id = '.$company_id.' and warehouse_status=2 or warehouse_status=3'; } if($type==-1) // all { $condition='company_id = '.$company_id; } //~ if(isset($_SESSION) && $_SESSION['user_id']!=''){ //~ $condition="user_id=".$_SESSION['user_id']; //~ } //~ // $rows = $adminObj->db->get_results($sql); $sql= "select uc.country_name , umw.* from uflexit_countries as uc JOIN uflexit_master_wareHouse as umw ON umw.warehouse_country_id=uc.country_id where $condition "; $details = $wareHouseAdminObj->db->get_results($sql); $col = 'A'; $heading_array=array('S.No', 'Warehouse Number','Warehouse Name','Warehouse Id','Country','Status'); // setting the heading $i =1; foreach($heading_array as $heading) { $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$heading); } $j=1; //~ $details = array(); foreach($details as $row) { $col = 'A'; $i++; if($row['warehouse_status']==0) { $status= "pending"; } if($row['warehouse_status']==2) { $status= "Rejected"; } if($row['warehouse_status']==3) { $status= "Blocked"; } if($row['warehouse_status']==1) { $status= "Active"; } $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$j++); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$row['warehouse_number']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['warehouse_name']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['warehouse_id']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['country_name']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$status); } // $spreadsheet->getActiveSheet() // ->setCellValue('C6', '=SUM(C1:C5)'); // $spreadsheet->getActiveSheet() // ->getStyle("C6")->getFont() // ->setBold(true); $writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possible // $writer->save("my_excel_file.xlsx"); $writer->save('php://output'); ?>