Excel file extension issue

Excel cannot open the file because the extension is not valid verify that the file is not been corrupted and that file extension matches the format of the file

I’m currently using PHPExcel to print and I’m currently having a problem using it since the size of my excel is always 1kb.
Currently using excel 2019

<?php 
include 'functions.php';

if (!isAdmin()) {
	$_SESSION['msg'] = "You must log in first";
	header('location: ../login.php');
}


if (isset($_GET['logout'])) {
	session_destroy();
	unset($_SESSION['user']);
	header("location: ../login.php");
}
 	
/////////////////
$_SESSION['tax_year'] = (!empty($_SESSION['tax_year']) ? $_SESSION['tax_year'] : date('Y'));
$_SESSION['tax_year'] = (isset($_POST['tax_year']) ? $_POST['tax_year'] : $_SESSION['tax_year']);
///////////////// 
$years = array(); 
$sqlyears = "SELECT distinct `year` FROM `tax_amount` ORDER BY `year` ASC";
$resultyears = mysqli_query($db, $sqlyears);
while($row = mysqli_fetch_array($resultyears)){
	$years[] = $row['year'];
}
if(!in_array(date('Y', strtotime('last year')),$years)):
	$years[] = date('Y', strtotime('last year'));
endif;
	
if(!in_array(date('Y'),$years)):
	$years[] = date('Y');
endif;	
 	
if(!in_array(date('Y', strtotime('next year')),$years)):
	$years[] = date('Y', strtotime('next year'));
endif;

sort($years);

///////////////////////////////////
if(!empty($_SESSION['tax_year']) && in_array($_SESSION['tax_year'],$years)){
	$data = array(); 
	$query = "SELECT 
	  t.id
	, t.property_id
	, t.amount
	, t.year
	, t.payments_verified
	, p.id AS property_id
	, p.full_name	
	FROM tax_amount t
		LEFT JOIN `property` p
			ON t.property_id  = p.id
	WHERE t.payments_verified is NULL AND t.year = '".$_SESSION['tax_year']."' ORDER BY t.id DESC";	
	$result = $db->query($query);
	while($row = $result->fetch_assoc()){
		$data[$row['id']] = $row;
	}
}
///////////////////////////////////
$full_headings = array("full_name" => "Name", "amount" => "  Amount  ", "year" => "  Year  ");
///////////////////////////////////
if(isset($_POST['print_excel']) && !empty($data)):

		
	$lastcol = 'C';
	$lastrow = 3;
	
	require_once 'PHPExcel.php';
	$objPHPExcel = new PHPExcel();
	$objPHPExcel->removeSheetByIndex(0);
	$objWorkSheet = $objPHPExcel->createSheet(0); 
	
	$title_cells = array(); 
	$subtitle_cells = array(); 
	$heading_cells = array(); 	 
	
	$col = "A";
	$xrow = 1;	
	////////////////////////////////////	
	
	$dirPath = "temp"; 
	$File = "TAX_PAYMENTS_UNPAID".$_SESSION['tax_year'].".xlsx";   
	
	$FileName = $dirPath . "/" . $File;
	//////////////////////////////////// 
	 
	$cell = $col.$xrow;
	$cellLAST = $lastcol.$lastrow;
	$merge = $cell.':'.$cellLAST; 
	$objWorkSheet->mergeCells($merge);	
	$objWorkSheet->setCellValue($cell, "TAX PAYMENTS ".$_SESSION['tax_year']);
	$title_cells[] = $merge;
	
	$col = "A";
	$xrow = 4;
	  
	//Set row height
	$objWorkSheet->getRowDimension($xrow)->setRowHeight(22);	
	
	foreach($full_headings as $field => $heading):
	 
		$cell = $col.$xrow;
		$objWorkSheet->setCellValue($cell, $heading);
		$heading_cells[] = $cell; 
		$col++;
		
	endforeach;
	
	$col = "A";
	$xrow = 5;
	foreach($data as $id => $ar):
		
		$col = "A";	
		
		foreach($full_headings as $field => $heading):					
			$cell = $col.$xrow; 	
			if($field == "amount"):
				$objWorkSheet->getStyle($cell)
				->getNumberFormat()	
				->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER_00); 
			endif;
			$objWorkSheet->setCellValue($cell, $data[$id][$field]);
			$col++;
		endforeach;
		$xrow++;
	endforeach;
	
	
	// Set document properties//Edit as needed                            
	$objPHPExcel->getProperties()->setCreator($_SESSION['fullname'])
	                             ->setLastModifiedBy($_SESSION['fullname'])
	                             ->setTitle("Master Report ".$_SESSION['tax_year'])
	                             ->setSubject("Master Report ".$_SESSION['tax_year'])
	                             ->setDescription("Master Report ".$_SESSION['tax_year'])
	                             ->setKeywords("office PHPExcel php")
	                             ->setCategory("MasterReport");
								     
	foreach($title_cells as $tcell){                    
		$objWorkSheet->getStyle($tcell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objWorkSheet->getStyle($tcell)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 
		$objWorkSheet->getStyle($tcell)->applyFromArray(
	       array(
	               'fill' => array(
	                   'type' => PHPExcel_Style_Fill::FILL_SOLID,
	                   'color' => array('rgb' => 'FFFFFF')
	               ),
	               'font' => array(
	                   'name' => 'Arial',
	                   'size' => '15',
	                   'bold' => true,
	                   'color' => array('rgb' => '343434')
	               ),
	               'borders' => array(
	                   'allborders' => array(
	                   'style' => PHPExcel_Style_Border::BORDER_THIN,
	                   'color' => array('rgb' => '7A7A7A')
	               )
	           )
	       )
	   );
	}
	foreach($heading_cells as $hcell){                    
		$objWorkSheet->getStyle($hcell)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
		$objWorkSheet->getStyle($hcell)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER); 
		$objWorkSheet->getStyle($hcell)->applyFromArray(
	        array(
	                'fill' => array(
	                    'type' => PHPExcel_Style_Fill::FILL_SOLID,
	                    'color' => array('rgb' => 'FFE799')
	                ),
	                'font' => array(
	                    'name' => 'Calibri',
	                    'size' => '10',
	                    'bold' => true,
	                    'color' => array('rgb' => '343434')
	                ),
	                'borders' => array(
	                    'allborders' => array(
	                    'style' => PHPExcel_Style_Border::BORDER_THIN,
	                    'color' => array('rgb' => '000000')
						)
	            )
	        )
	    );
	}
	
	foreach(range('A','C') as $columnID) {
	    $objWorkSheet->getColumnDimension($columnID)
	        ->setAutoSize(true);
	}
	// Rename worksheet
	$objWorkSheet->setTitle("Master Report ".$_SESSION['tax_year']);
	
	// Set active sheet index to the first sheet, so Excel opens this as the first sheet
	$objPHPExcel->setActiveSheetIndex(0);  
	ob_end_clean();
	header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
	header('Content-Disposition: attachment;filename="'.$File.'"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
	$objWriter->save('php://output'); 
	exit();
	
endif;
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="style/approval.css">
    <link rel="stylesheet" type="text/css" href="style/jquery.autocomplete.css">
  <link rel="stylesheet" type="text/css" href="../bootstrap-5.1.3-dist/css/bootstrap.min.css">
  <link rel="stylesheet" type="text/css" href="../jquery/jquery.min.js">
  <link rel="stylesheet" type="text/css" href="../bootstrap-5.1.3-dist/js/bootstrap.min.js">
  <link rel="stylesheet" type="text/css" href="../font-awesome/css/font-awesome.min.css">

    <title>Records</title>
</head>
<body>
	
<div class="pageheader">
	<img src="../images/luna_banner.png" border="0" width="100%" height="160" alt="luna banner">
</div>
<?php 
include 'navbar.php';

echo '<div class="searchbar">
	<form action="" method="post">	
		<table border=0>
			<tr>				
				<td class="textcenter">Tax Year</td>		
				
				<td>&nbsp;Generate&nbsp;</td>		
			</tr>
			<tr>
				<td><select name="tax_year" onchange="this.form.submit()">
						<option value="">Select Tax Year</option>'."\r";
						foreach($years as $year):
							$selected_year = (isset($_SESSION['tax_year']) && $_SESSION['tax_year'] == $year ? ' selected="selected"' : '');
							echo '<option value="'.$year.'"'.$selected_year.'>Tax Year '.$year.'</option>'."\r";
						endforeach;
				echo '</select></td>
				
				<td><input type="submit" name="print_excel" value="Generate Excel" /></td>	
			</tr>
		</table>
	</form>
</div>'."\r";
?> 	

<div class="center" style="margin:20px auto;">
   <p class="ofl_title">Unpaid Tax Payer</p>
</div>


<table class="report-table">
	<thead class="ofl_th">
		<tr>
			<th>Name</th>
			<th>Amount</th>
			<th>Year</th>
		</tr>
	</thead>
	<?php
	if(!empty($data)):
		foreach($data as $row):		 
		echo '<tr>
			<td style="text-transform: uppercase; font-size: 15px;">'.$row['full_name'].'</td>
			<td style="text-transform: uppercase; font-size: 15px;">'.$row['amount'].'</td>
			<td style="text-transform: uppercase; font-size: 15px;">'.$row['year'].'</td>
		</tr>'."\r";
		endforeach;
	endif;
	?>
</table>


<div class="center" style="margin:20px auto;">
   <p class="ofl_title">Unpaid Tax Payer</p>
</div>

<div id="divToPrint" style="display:none;">
	<table class="report-table">
		<thead class="ofl_th">
			<tr>
				<th>Name</th>
				<th>Amount</th>
				<th>Year</th>
			</tr>
		</thead>
		<?php
		if(!empty($data)):
			foreach($data as $row):		 
			echo '<tr>
				<td style="text-transform: uppercase; font-size: 15px;">'.$row['full_name'].'</td>
				<td style="text-transform: uppercase; font-size: 15px;">'.$row['amount'].'</td>
				<td style="text-transform: uppercase; font-size: 15px;">'.$row['year'].'</td>
			</tr>'."\r";
			endforeach;
		endif;
		?>
	</table>
</div>

<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>
<br>

<?php require ("footer.php");?> 
</body>
</html>

Check its contents with any plain text editor, it may contain PHP error message that will be helpful

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.