JezK
Edit File: export-materialTransactions.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="MaterialTransaction.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); } // $rows = $adminObj->db->get_results($sql); // print_r($_GET); if(isset($_GET['wid']) && $_GET['wid']!='') { $wid=$_GET['wid']; $condition = "(umt.warehouse_id=$wid)"; } $sql= "select umt.itemcode,umt.qty,umt.item_desc, umt.quotation_id,umt.warehouse_id,umt.type,umt.randval,umw.warehouse_name from uflexit_material_transactions as umt JOIN uflexit_master_warehouse as umw ON umw.warehouse_id=umt.warehouse_id WHERE $condition "; $details = $adminObj->db->get_results($sql); $col = 'A'; $heading_array=array('S.No','Quatation ID','Warehouse ID','Warehouse Name','Item No','Item Description','Quantity','Transaction type '); // setting the heading $i =1; foreach($heading_array as $heading) { $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$heading); } $j=1; if($details){ foreach($details as $row) { $col = 'A'; $i++; if($row['type']==1){ $row['type']="Issued"; }else{ $row['type']="Received"; } $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$j++); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$row['quotation_id']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['warehouse_id']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['warehouse_name']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['itemcode']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['item_desc']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['qty']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['type']); } } $writer = IOFactory::createWriter($spreadsheet, "Xlsx"); //Xls is also possible // $writer->save("my_excel_file.xlsx"); $writer->save('php://output'); ?>