JezK
Edit File: export_invoices.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="INVOICES_LIST.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); } // print_r($_SESSION); if(isset($_REQUEST['wid']) && $_REQUEST['wid'] != ""){ $condition = " uin.warehouse_id=".$_REQUEST['wid'].""; }else{ $condition = " uin.company_id=".$_SESSION['company_id'].""; } //~ if(isset($_SESSION) && $_SESSION['user_id']!=''){ //~ $condition="user_id=".$_SESSION['user_id']; //~ } //~ // $rows = $adminObj->db->get_results($sql); $sql= "select uct.currency,ur.role_name,uc.first_name,uc.role_id,uc.last_name,uin.invoice_id,uin.invoice_number,uin.user_id,uin.quotation_id,uin.grandtotal,uin.warehouse_id from uflexit_users as uc JOIN uflexit_invoice as uin ON uc.user_id=uin.user_id JOIN uflexit_roles as ur on ur.role_id= uc.role_id join uflexit_request_quote as urq on urq.quotation_id=uin.quotation_id join uflexit_countries as uct on uct.country_id=urq.currency_type where $condition "; $details = $wareHouseAdminObj->db->get_results($sql); // print_r($details); $col = 'A'; $heading_array=array('S.No','Invoice Number','Customer Name','Customer Type','Quotation Id','Value'); // 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++; $row['customer_name']=$row['first_name'].' '. $row['last_name']; $row['customer_type']=$row['role_name']; //~ $row['invoice_value']=$row['invoice_value']." ".$row['currency']; $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$j++); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['invoice_number']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$row['customer_name']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i ,$row['customer_type']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i,$row['quotation_id']); $spreadsheet->getActiveSheet(0)->setCellValue($col++.$i, $row['grandtotal']); } // $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'); ?>